:root {
  --cr-blue: #003b7a;
  --cr-blue-2: #0059c8;
  --cr-navy: #001f47;
  --cr-gold: #d59016;
  --cr-bg: #f3f7fb;
  --cr-card: #ffffff;
  --cr-line: #d8e3ef;
  --cr-muted: #60718a;
  --cr-good: #15803d;
  --cr-warn: #b45309;
  --cr-bad: #b91c1c;
  --shadow: 0 18px 48px rgba(0, 31, 71, 0.18);
  --radius: 18px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font);
  background: radial-gradient(circle at top left, #eef6ff, var(--cr-bg));
  color: var(--cr-navy);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 800;
  transition: transform 0.15s ease, filter 0.15s ease, box-shadow 0.15s ease;
}

button:hover {
  transform: translateY(-1px);
  filter: brightness(1.02);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

input,
select {
  width: 100%;
  border: 1px solid var(--cr-line);
  border-radius: 12px;
  padding: 12px 13px;
  background: #ffffff;
  color: var(--cr-navy);
}

label {
  display: block;
  margin-bottom: 6px;
  color: var(--cr-muted);
  font-weight: 700;
  font-size: 0.9rem;
}

h1,
h2,
h3 {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.8rem);
  letter-spacing: -0.035em;
}

h2 {
  font-size: 1.45rem;
  letter-spacing: -0.02em;
}

h3 {
  font-size: 1.1rem;
}

p {
  color: var(--cr-muted);
  line-height: 1.5;
}

/* General app shell */

.page-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
}

.sidebar {
  background: linear-gradient(180deg, var(--cr-navy), var(--cr-blue));
  color: #ffffff;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.logo-block {
  display: grid;
  grid-template-columns: 54px 1fr;
  gap: 10px;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.18);
}

.logo-mark {
  width: 54px;
  height: 54px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: #ffffff;
  color: var(--cr-blue);
  font-size: 2rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}

.logo-title {
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: 0.5px;
}

.logo-sub {
  font-size: 0.76rem;
  opacity: 0.78;
  margin-top: 2px;
}

.nav-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.nav-item {
  color: #ffffff;
  text-decoration: none;
  padding: 13px 14px;
  border-radius: 13px;
  display: flex;
  gap: 10px;
  align-items: center;
  font-weight: 800;
  opacity: 0.88;
}

.nav-item.active,
.nav-item:hover {
  background: rgba(255, 255, 255, 0.14);
  opacity: 1;
}

.main {
  padding: 28px;
}

.topbar {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(216, 227, 239, 0.8);
  border-radius: 24px;
  padding: 18px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  box-shadow: 0 10px 28px rgba(0, 31, 71, 0.08);
  margin-bottom: 22px;
}

.eyebrow {
  color: var(--cr-gold);
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.78rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 22px;
  align-items: start;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.card {
  background: var(--cr-card);
  border: 1px solid var(--cr-line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 31, 71, 0.08);
  padding: 20px;
}

.card-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 11px;
  border-radius: 999px;
  background: #eaf2ff;
  color: var(--cr-blue);
  font-weight: 900;
  font-size: 0.78rem;
  text-transform: uppercase;
}

.status-open {
  background: #dcfce7;
  color: #166534;
}

.status-running {
  background: #fee2e2;
  color: #991b1b;
}

.status-closed,
.status-paused {
  background: #fef3c7;
  color: #92400e;
}

.status-completed {
  background: #ede9fe;
  color: #5b21b6;
}

.primary-btn {
  background: linear-gradient(135deg, var(--cr-blue-2), var(--cr-blue));
  color: #ffffff;
  box-shadow: 0 12px 28px rgba(0, 89, 200, 0.24);
}

.secondary-btn {
  background: #ffffff;
  color: var(--cr-blue);
  border: 1px solid var(--cr-blue-2);
}

.gold-btn {
  background: #fff7e6;
  color: #9a6508;
  border: 1px solid #e5ad3b;
}

.danger-btn {
  background: #fee2e2;
  color: var(--cr-bad);
  border: 1px solid #fecaca;
}

.form-grid {
  display: grid;
  gap: 14px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.race-list {
  display: grid;
  gap: 14px;
}

.race-card {
  border: 1px solid var(--cr-line);
  border-radius: 16px;
  padding: 16px;
  background: #fbfdff;
  display: grid;
  gap: 12px;
}

.race-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--cr-muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.horse-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  color: #ffffff;
  font-weight: 900;
  flex: 0 0 auto;
}

.horse-blue {
  background: #0f62c4;
}

.horse-orange {
  background: #f97316;
}

.horse-green {
  background: #16a34a;
}

.horse-pink {
  background: #db2777;
}

.horse-teal {
  background: #0891b2;
}

.horse-purple {
  background: #7c3aed;
}

.horse-gold {
  background: #d97706;
}

.horse-brown {
  background: #92400e;
}

.horse-red {
  background: #dc2626;
}

.horse-navy {
  background: #1e3a8a;
}

.horse-lime {
  background: #65a30d;
}

.horse-silver {
  background: #64748b;
}

.assignment-box {
  border: 2px solid #bfd7ff;
  background: linear-gradient(135deg, #f8fbff, #eaf2ff);
  border-radius: 18px;
  padding: 18px;
  display: none;
}

.assignment-box.show {
  display: block;
}

.message {
  padding: 12px 14px;
  border-radius: 13px;
  background: #eef6ff;
  color: var(--cr-blue);
  font-weight: 800;
  display: none;
}

.message.show {
  display: block;
}

.message.error {
  background: #fee2e2;
  color: var(--cr-bad);
}

/* Derby display page */

.display-shell {
  min-height: 100vh;
  background: linear-gradient(135deg, #001f47, #003b7a 38%, #eef6ff 38%);
  padding: 10px;
}

.display-board {
  min-height: calc(100vh - 20px);
  background: #ffffff;
  border-radius: 22px;
  border: 1px solid #d8e3ef;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.display-header {
  padding: 12px 24px 14px;
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr) 260px;
  gap: 18px;
  align-items: center;
  border-bottom: 1px solid var(--cr-line);
  background: linear-gradient(180deg, #ffffff, #f8fbff);
}

.display-title {
  text-align: center;
}

.display-title h1 {
  font-size: clamp(2rem, 4vw, 3.6rem);
  line-height: 1;
}

.display-title p {
  margin: 6px 0 0;
  font-weight: 800;
  color: var(--cr-blue-2);
}

.weather-box {
  display: flex;
  justify-content: flex-end;
  gap: 18px;
  align-items: center;
  color: var(--cr-blue);
  font-weight: 900;
}

.weather-icon {
  font-size: 2.1rem;
  color: var(--cr-gold);
}

/* Main race row */

.race-stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 350px;
  gap: 18px;
  padding: 12px 16px 8px;
  min-height: 0;
  align-items: flex-start;
}

.canvas-card {
  position: relative;
  border-radius: 18px;
  overflow: hidden;
  background: #b7d8f2;
  height: 405px;
  min-height: 405px;
  max-height: 405px;
  border: 1px solid var(--cr-line);
}

#raceCanvas {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
}

/* Old overlay positions kept for compatibility, but the active cards are now below the track */

.overlay-top-left,
.overlay-top-right,
.overlay-bottom-left,
.roll-overlay {
  position: absolute;
  z-index: 5;
}

.overlay-top-left {
  top: 18px;
  left: 18px;
}

.overlay-top-right {
  top: 18px;
  right: 18px;
}

.overlay-bottom-left {
  left: 18px;
  bottom: 18px;
}

.canvas-card > .overlay-bottom-left {
  display: none;
}

/* Live / dice / progress cards */

.live-badge {
  background: rgba(0, 31, 71, 0.96);
  color: #ffffff;
  border-radius: 14px;
  padding: 12px 16px;
  display: grid;
  gap: 4px;
  box-shadow: 0 12px 24px rgba(0, 31, 71, 0.18);
  font-weight: 900;
}

.live-dot {
  color: #ef4444;
}

.distance-box {
  background: rgba(0, 31, 71, 0.96);
  color: #ffffff;
  border-radius: 14px;
  padding: 14px 18px;
  min-width: 190px;
  box-shadow: 0 12px 24px rgba(0, 31, 71, 0.18);
}

.distance-large {
  font-size: 2.2rem;
  font-weight: 1000;
  letter-spacing: -0.04em;
}

.progress-track {
  width: 100%;
  height: 10px;
  background: rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 8px;
}

.progress-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #f59e0b, #fde68a);
  border-radius: inherit;
}

.roll-overlay {
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid var(--cr-line);
  border-radius: 18px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 12px 30px rgba(0, 31, 71, 0.16);
}

.die {
  width: 54px;
  height: 54px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  font-size: 1.8rem;
  font-weight: 1000;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.13), 0 8px 18px rgba(0, 0, 0, 0.15);
}

.die-red {
  background: #dc2626;
  color: #ffffff;
}

.die-white {
  background: #ffffff;
  color: var(--cr-navy);
  border: 1px solid #cbd5e1;
}

.roll-text {
  font-weight: 900;
  color: var(--cr-navy);
}

/* Right information panel */

.info-panel {
  height: 405px;
  max-height: 405px;
  overflow: hidden;
  display: grid;
  grid-template-rows: 44px 70px 1fr;
  border: 1px solid var(--cr-line);
  border-radius: 18px;
  background: #ffffff;
  box-shadow: 0 10px 24px rgba(0, 31, 71, 0.09);
}

.info-panel-header {
  background: var(--cr-blue);
  color: #ffffff;
  padding: 12px 14px;
  text-align: center;
  font-weight: 1000;
  letter-spacing: 0.04em;
  line-height: 1;
}

.info-section {
  padding: 10px 12px;
  border-bottom: 1px solid var(--cr-line);
  min-height: 0;
  overflow: hidden;
}

.info-section:last-child {
  border-bottom: 0;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  text-align: center;
  align-items: center;
}

.kpi-value {
  font-size: 1.55rem;
  line-height: 1;
  font-weight: 1000;
  color: var(--cr-blue);
  margin-top: 2px;
}

.kpi-label {
  color: var(--cr-muted);
  font-size: 0.68rem;
  line-height: 1;
  font-weight: 800;
  text-transform: uppercase;
}

.info-panel h3 {
  margin: 0 0 8px !important;
  font-size: 1rem;
  line-height: 1;
}

#miniMap {
  width: 100%;
  height: 265px;
  max-height: 265px;
  display: block;
}

/* Old side supporter footer hidden if it exists by mistake */

.info-panel .total-supporters {
  display: none !important;
}

/* Optional legacy leaderboard support, hidden in current display */

.leaderboard {
  background: rgba(0, 31, 71, 0.9);
  color: #ffffff;
  border-radius: 16px;
  min-width: 330px;
  overflow: hidden;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
}

.lb-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 62px;
  gap: 9px;
  align-items: center;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  font-weight: 800;
}

.lb-row:last-child {
  border-bottom: 0;
}

.lb-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lb-pos {
  text-align: right;
  opacity: 0.9;
}

.race-leaderboard-strip {
  display: none !important;
}

/* Supporters bar below the track */

.supporters-bar {
  width: calc(100% - 32px);
  margin: 8px 16px 6px;
  background: #ffffff;
  border: 1px solid #d7e3f2;
  border-radius: 16px;
  box-shadow: 0 8px 22px rgba(8, 39, 84, 0.08);
  padding: 8px 16px;
}

.supporters-bar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}

.supporters-bar-header h3 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.1;
  color: #0b2f6b;
}

.total-supporters-inline {
  background: #0b4a94;
  color: #ffffff;
  border-radius: 999px;
  padding: 6px 12px;
  font-weight: 900;
  font-size: 0.88rem;
  line-height: 1;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.supporter-list {
  display: grid;
  gap: 8px;
}

.supporter-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 50px;
  gap: 8px;
  align-items: center;
  font-weight: 800;
}

.supporters-bar-list {
  display: grid !important;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px 18px;
}

.supporters-bar-list .supporter-row {
  display: grid !important;
  grid-template-columns: 24px 1fr 24px !important;
  align-items: center;
  gap: 7px;
  padding: 1px 0 !important;
  font-size: 0.82rem !important;
  line-height: 1 !important;
}

.supporters-bar-list .supporter-row .horse-badge,
.supporters-bar-list .supporter-row span.horse-badge {
  width: 21px !important;
  height: 21px !important;
  font-size: 0.65rem !important;
}

/* Bottom race status strip */

.race-status-strip {
  display: grid;
  grid-template-columns: 180px minmax(380px, 560px) 210px;
  gap: 16px;
  align-items: center;
  justify-content: center;
  padding: 8px 20px;
  margin-top: 4px;
  background: #f7fbff;
  border-top: 1px solid #d9e7f5;
}

/* Cards are static here, not absolute overlays */

.race-status-strip .live-badge,
.race-status-strip .roll-overlay,
.race-status-strip .distance-box {
  position: static !important;
  top: auto !important;
  right: auto !important;
  left: auto !important;
  bottom: auto !important;
  transform: none !important;
}

.race-status-strip .live-badge {
  width: 170px;
  min-height: 62px;
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 0.95rem;
  gap: 5px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.race-status-strip .roll-overlay {
  min-height: 66px;
  padding: 10px 18px;
  border-radius: 16px;
  display: grid;
  grid-template-columns: 52px 52px minmax(200px, 1fr);
  gap: 12px;
  align-items: center;
}

.race-status-strip .die {
  width: 50px;
  height: 50px;
  font-size: 1.65rem;
  border-radius: 12px;
}

.race-status-strip .die-red {
  background: #dc2626;
  color: #ffffff;
}

.race-status-strip .die-white {
  background: #f8fafc;
  color: #002b5c;
  border: 2px solid #cbd5e1;
}

.race-status-strip .roll-text {
  font-size: 1rem;
  font-weight: 1000;
  color: #002b5c;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.race-status-strip .distance-box {
  width: 200px;
  min-height: 70px;
  padding: 12px 16px;
  border-radius: 14px;
}

.race-status-strip .distance-large {
  font-size: 1.85rem;
  line-height: 1;
  font-weight: 1000;
  margin: 5px 0 7px;
}

.race-status-strip .progress-track {
  height: 8px;
}

/* Winner overlay */

.winner-banner {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  place-items: center;
  background: rgba(0, 31, 71, 0.70);
  backdrop-filter: blur(3px);
  color: #ffffff;
  text-align: center;
  padding: 24px;
}

.winner-banner.show {
  display: grid;
}

.winner-card {
  background: linear-gradient(135deg, #003b7a, #001f47);
  border: 2px solid #f4c765;
  border-radius: 28px;
  padding: 34px;
  min-width: min(720px, 90vw);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
}

.winner-card h2 {
  font-size: clamp(2rem, 6vw, 5rem);
  color: #ffffff;
}

.winner-card p {
  color: #fde68a;
  font-size: 1.25rem;
  font-weight: 900;
}

/* Display controls */

.display-controls {
  padding: 8px 22px 10px;
  border-top: 1px solid var(--cr-line);
  display: flex;
  gap: 12px;
  justify-content: center;
  background: #fbfdff;
}

.display-controls button {
  min-width: 160px;
  padding-top: 10px;
  padding-bottom: 10px;
}

/* Admin */

.admin-layout {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.small-text {
  font-size: 0.85rem;
  color: var(--cr-muted);
}

.table-like {
  display: grid;
  gap: 8px;
}

.table-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  border-bottom: 1px solid var(--cr-line);
  padding: 8px 0;
}

/* Responsive */

@media (max-width: 1200px) {
  .supporters-bar-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 1100px) {
  .page-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .grid-2,
  .admin-layout {
    grid-template-columns: 1fr;
  }

  .display-header {
    grid-template-columns: 1fr;
    text-align: center;
  }

  .weather-box {
    justify-content: center;
  }

  .race-stage {
    grid-template-columns: 1fr;
  }

  .canvas-card,
  .info-panel {
    height: 360px;
    max-height: 360px;
  }

  .race-status-strip {
    grid-template-columns: 1fr;
  }

  .race-status-strip .live-badge,
  .race-status-strip .roll-overlay,
  .race-status-strip .distance-box {
    width: 100%;
  }

  .race-status-strip .roll-overlay {
    grid-template-columns: 52px 52px 1fr;
  }
}

@media (max-width: 760px) {
  .main {
    padding: 16px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .form-row,
  .grid-3 {
    grid-template-columns: 1fr;
  }

  .display-shell {
    padding: 0;
  }

  .display-board {
    border-radius: 0;
    min-height: 100vh;
  }

  .display-controls {
    flex-wrap: wrap;
  }

  .leaderboard {
    min-width: 260px;
  }

  .roll-overlay {
    left: 12px;
    right: 12px;
    transform: none;
    justify-content: center;
  }

  .supporters-bar-list {
    grid-template-columns: 1fr;
  }

  .supporters-bar-header {
    flex-direction: column;
    align-items: flex-start;
  }
}