:root {
  --bg: #f4f7f3;
  --panel: #ffffff;
  --panel-alt: #eef5ef;
  --ink: #12231b;
  --muted: #5d6f69;
  --line: #dfeae3;
  --primary: #1b7f5a;
  --primary-strong: #126247;
  --accent: #d8f1d8;
  --warning: #c73939;
  --shadow: 0 12px 28px rgba(18, 35, 27, 0.08);
  --radius: 18px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: Inter, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(180deg, #eff6f2 0%, #edf2ee 100%);
  color: var(--ink);
}

body {
  min-height: 100vh;
}

button, input, select {
  font: inherit;
}

button {
  cursor: pointer;
}

.page-shell {
  max-width: 1600px;
  margin: 0 auto;
  padding: 20px 20px 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.72);
  backdrop-filter: blur(10px);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 46px;
  height: 46px;
  border-radius: 16px;
  background: linear-gradient(135deg, #1c8a65, #143f35);
  display: grid;
  place-items: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.15);
}

.brand-mark svg {
  width: 26px;
  height: 26px;
}

.brand-copy {
  min-width: 0;
}

.brand-kicker,
.eyebrow,
.race-header-kicker {
  margin: 0;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--primary-strong);
  font-weight: 700;
}

.brand-copy h1 {
  margin: 2px 0 0;
  font-size: clamp(1.45rem, 2vw, 2rem);
  line-height: 1.1;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.primary-btn, .secondary-btn, .ghost-btn, .file-button {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 0.78rem 1.05rem;
  font-weight: 700;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.primary-btn:active, .secondary-btn:active, .ghost-btn:active, .file-button:active {
  transform: translateY(1px);
}

.primary-btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-strong));
  color: #fff;
  box-shadow: 0 12px 18px rgba(24, 119, 81, 0.2);
}

.secondary-btn {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
}

.ghost-btn {
  background: #edf6ef;
  color: var(--ink);
  border: 1px solid #d0e2d6;
}

.file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
}

.admin-link {
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  padding: 0.5rem 0.7rem;
  border-radius: 10px;
}

.admin-link:hover {
  background: rgba(18, 35, 27, 0.04);
}

.page-layout {
  display: flex;
  flex-direction: column;
  gap: 18px;
  margin-top: 18px;
}

#controls {
  width: 100%;
}

.content-row {
  display: flex;
  width: 100%;
  min-width: 0;
  align-items: stretch;
}

#viewerShell {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
}

.welcome-hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: clamp(1.4rem, 2vw, 2rem);
  border-radius: 28px;
  background: linear-gradient(135deg, rgba(24, 132, 92, 0.11), rgba(255,255,255,0.6));
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.hero-copy {
  max-width: 680px;
}

.hero-copy h2 {
  margin: 0.18rem 0 0.8rem;
  font-size: clamp(2.2rem, 4vw, 4rem);
  line-height: 0.96;
  letter-spacing: -0.05em;
}

.hero-copy p:last-child {
  margin: 0;
  max-width: 600px;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.6;
}

.hero-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-panel {
  background: rgba(255,255,255,0.72);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: auto;
  min-height: 0;
  flex: 0 0 auto;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  height: auto;
  min-height: 0;
  flex: 0 0 auto;
}

.toolbar-row + .toolbar-row {
  margin-top: 12px;
}

.toolbar-row-primary {
  justify-content: flex-start;
}

.toolbar-row-tertiary {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.speed-field,
.map-select,
.field,
.upload-inline,
.filter-panel label {
  display: inline-flex;
  flex-direction: column;
  gap: 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.speed-field input,
.map-select select,
.field input,
.field select,
.filter-panel input,
.filter-panel select,
.upload-inline input,
#boundsInput,
#speedInput {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--ink);
  padding: 0.7rem 0.8rem;
  font-size: 0.95rem;
}

.speed-field {
  min-width: 110px;
}

#speedInput {
  width: 92px;
}

.race-speed-field {
  min-width: 92px;
}

.race-speed-field #speedInput {
  width: 92px;
}

.timeline-block {
  flex: 1 1 240px;
  min-width: 180px;
}

#timeRange {
  width: 100%;
  accent-color: var(--primary);
}

.time-badge {
  min-width: 86px;
  text-align: center;
  padding: 0.5rem 0.7rem;
  border-radius: 999px;
  background: var(--panel-alt);
  border: 1px solid var(--line);
  font-weight: 800;
  color: var(--ink);
}

.playback-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.filter-panel,
.race-panel {
  display: none;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
  padding: 0;
  margin: 0;
  height: auto;
  min-height: 0;
  flex: 0 0 auto;
}

.sidebar .filter-panel {
  display: none;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  width: 100%;
}

.sidebar .sidebar-filter-wrap {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.filter-panel label,
.field,
.upload-inline,
.map-select {
  min-width: 140px;
}

.filter-panel input,
.filter-panel select,
.field input,
.field select,
.upload-inline input,
#boundsInput {
  min-height: 42px;
}

.race-panel {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 0;
  gap: 8px;
  height: auto;
  min-height: 0;
  flex: 0 0 auto;
}

.panel-header {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.race-tool-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}

.race-tool-grid > * {
  flex: 0 0 auto;
}

.compact-field {
  min-width: 0;
}

.file-dropzone {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 64px;
  border: 1.5px dashed #b9d4c2;
  border-radius: 16px;
  background: linear-gradient(180deg, #f5faf7, #eef7f0);
  color: var(--ink);
  padding: 10px 12px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.file-dropzone:hover {
  border-color: var(--primary);
}

.file-dropzone input {
  display: none;
}

.dropzone-copy {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.95rem;
}

.dropzone-copy small {
  color: var(--muted);
}

.status-banner {
  display: none;
  margin-top: 0;
  padding: 0.5rem 0.7rem;
  border-radius: 12px;
  background: #edf7f0;
  color: var(--primary-strong);
  border: 1px solid #d7ecd9;
  font-weight: 700;
}

.status-banner.error {
  background: #fff1f1;
  color: var(--warning);
  border-color: #f2c2c2;
}

.viewer-shell {
  display: grid;
  grid-template-columns: minmax(260px, 350px) minmax(0, 1fr);
  gap: 18px;
  width: 100%;
  min-height: 680px;
  flex: 1 1 auto;
  min-width: 0;
}

.sidebar {
  background: rgba(255,255,255,0.8);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: width 0.2s ease, padding 0.2s ease, min-width 0.2s ease;
}

.viewer-shell.sidebar-collapsed {
  grid-template-columns: 52px minmax(0, 1fr);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px 12px;
}

.sidebar-header h3 {
  margin: 0;
  font-size: 1.1rem;
}

#toggleSidebarBtn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 10px;
  width: 36px;
  height: 36px;
  font-size: 1rem;
}

.sidebar-content {
  height: calc(100% - 44px);
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#tracksList {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.track-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1.3fr) auto auto auto auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, #ffffff, #f7faf8);
}

.track-color-input {
  width: 24px;
  height: 24px;
  border: 1px solid rgba(18,35,27,0.15);
  border-radius: 8px;
  background: transparent;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}

.track-color-input::-webkit-color-swatch-wrapper { padding: 0; }
.track-color-input::-webkit-color-swatch { border: none; border-radius: 7px; }

.track-item input[type="text"],
.track-item select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  padding: 0.55rem 0.65rem;
  font-size: 0.82rem;
  min-width: 0;
}

.track-item .track-name-input {
  min-width: 0;
}

.track-item select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) calc(50% - 2px), calc(100% - 11px) calc(50% - 2px);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 26px;
}

.track-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.track-item .initials-input {
  max-width: 52px;
  text-align: center;
}

.map-area {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: rgba(255,255,255,0.76);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 14px;
  box-shadow: var(--shadow);
}

.race-header {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 6px 2px 12px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 12px;
}

.race-header-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
}

.race-header-copy {
  min-width: 0;
}

.race-header h3 {
  margin: 0.2rem 0 0;
  font-size: clamp(1.1rem, 1.7vw, 1.8rem);
}

.race-header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.9rem;
  flex-wrap: wrap;
}

.race-header-controls {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

.race-header-controls .primary-btn,
.race-header-controls .secondary-btn {
  min-width: 92px;
}

.race-header-controls .speed-field {
  margin-left: 2px;
}

.meta-divider {
  opacity: 0.3;
}

#map {
  flex: 1;
  min-height: 560px;
  width: 100%;
  border-radius: 18px;
  overflow: hidden;
}

.leaflet-container {
  background: #e8efe8;
  border-radius: 18px;
}

.sidebar.collapsed {
  width: 52px;
  min-width: 52px;
  max-width: 52px;
  padding: 10px 8px;
}

.sidebar.collapsed .sidebar-content {
  display: none;
}

.sidebar.collapsed .sidebar-header {
  justify-content: center;
  padding: 0 0 12px;
}

.sidebar.collapsed .sidebar-header h3 {
  display: none;
}

.sidebar.collapsed #toggleSidebarBtn {
  transform: rotate(-90deg);
}

.track-marker-icon .marker-label {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 11px;
  color: #fff;
  font-weight: 800;
  font-size: 10px;
  border: 1px solid rgba(14, 22, 18, 0.4);
  box-shadow: 0 2px 6px rgba(0,0,0,0.35);
}

.track-marker-icon {
  pointer-events: none;
}

@media (max-width: 980px) {
  .page-layout {
    flex-direction: column;
  }

  #controls {
    width: 100%;
    flex-basis: auto;
  }

  .content-row {
    width: 100%;
  }

  .viewer-shell,
  .viewer-shell.sidebar-collapsed {
    grid-template-columns: 1fr;
  }

  .sidebar {
    max-height: 320px;
  }

  .sidebar.collapsed {
    width: 52px;
    min-width: 52px;
    max-width: 52px;
  }

  .map-area {
    min-height: 420px;
  }
}

@media (max-width: 768px) {
  .page-shell {
    padding: 14px 12px 16px;
  }

  .topbar {
    padding: 14px 14px;
    border-radius: 18px;
  }

  .brand-copy h1 {
    font-size: 1.2rem;
  }

  .brand-kicker {
    letter-spacing: 0.08em;
  }

  .welcome-hero {
    flex-direction: column;
    align-items: flex-start;
    border-radius: 22px;
  }

  .hero-copy h2 {
    font-size: clamp(2.1rem, 12vw, 3rem);
  }

  .hero-actions {
    width: 100%;
  }

  .hero-actions .primary-btn,
  .hero-actions .secondary-btn {
    flex: 1 1 180px;
  }

  .toolbar-panel {
    padding: 12px 12px 14px;
    border-radius: 18px;
  }

  .toolbar-row,
  .toolbar-row-tertiary {
    gap: 10px;
  }

  .toolbar-row > * {
    flex: 1 1 100%;
  }

  .timeline-block {
    flex-basis: 100%;
  }

  .filter-panel,
  .race-panel {
    padding-top: 12px;
  }

  .race-tool-grid {
    grid-template-columns: 1fr;
  }

  .field,
  .compact-field,
  .map-select,
  .upload-inline,
  .filter-panel label {
    min-width: 0;
    width: 100%;
  }

  .sidebar {
    border-radius: 18px;
    max-height: 260px;
  }

  .map-area {
    padding: 10px;
    border-radius: 18px;
  }

  .race-header {
    align-items: flex-start;
  }

  .race-header-main,
  .race-header-controls,
  .playback-bar {
    width: 100%;
  }

  .race-header-main {
    flex-direction: column;
    align-items: flex-start;
  }

  .race-header-controls {
    justify-content: flex-start;
    margin-left: 0;
  }

  .race-header-controls .primary-btn,
  .race-header-controls .secondary-btn,
  .race-speed-field {
    flex: 0 0 auto;
  }

  .playback-bar {
    flex-wrap: wrap;
  }

  .timeline-block {
    flex-basis: 100%;
    min-width: 0;
  }

  #map {
    min-height: 360px;
  }
}

@media (max-width: 520px) {
  .topbar {
    flex-wrap: wrap;
  }

  .topbar-actions {
    width: 100%;
    justify-content: space-between;
  }

  .topbar-actions .primary-btn {
    flex: 1 1 auto;
  }

  .track-item {
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "color name name initials"
      "color gender category visible";
  }

  .track-color-input { grid-area: color; }
  .track-name-input { grid-area: name; }
  .track-item select:nth-of-type(1) { grid-area: gender; }
  .track-item select:nth-of-type(2) { grid-area: category; }
  .initials-input { grid-area: initials; }
  .track-item input[type="checkbox"] { grid-area: visible; }
}
