*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --topbar-h: 50px;
  --sidebar-width: 300px;
  --accent: #0071E3;
  --accent-light: rgba(0, 113, 227, 0.1);
  --text-primary: #1d1d1f;
  --text-secondary: #6e6e73;
  --text-muted: #aeaeb2;
  --border: rgba(0, 0, 0, 0.08);
  --bg-hover: rgba(0, 0, 0, 0.04);
  --shadow-sm: 0 1px 4px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 24px rgba(0,0,0,0.09), 0 2px 8px rgba(0,0,0,0.05);
  --shadow-lg: 0 20px 60px rgba(0,0,0,0.14), 0 4px 16px rgba(0,0,0,0.08);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', sans-serif;
  font-size: 14px;
  color: var(--text-primary);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  display: flex;
  flex-direction: column;
}

/* ── Top bar ──────────────────────────────────────────────── */

#topbar {
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 16px;
  padding-right: 12px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-bottom: 1px solid var(--border);
  z-index: 900;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
}

#topbar-nav {
  display: flex;
  align-items: center;
  gap: 2px;
}

.nav-link {
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 6px 12px;
  border-radius: 8px;
  letter-spacing: -0.01em;
  transition: background var(--transition), color var(--transition);
}

.nav-link:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.nav-link.active {
  background: var(--accent-light);
  color: var(--accent);
}

#topbar-profile {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--text-secondary);
  padding: 4px 10px 4px 4px;
  border-radius: 20px;
  transition: background var(--transition), color var(--transition);
}

#topbar-profile:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

#topbar-profile:hover img {
  box-shadow: 0 0 0 2px var(--accent);
}

#topbar-profile-name {
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.01em;
}

#topbar-profile img {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  flex-shrink: 0;
  box-shadow: 0 0 0 1.5px var(--border);
  transition: box-shadow var(--transition);
}

/* ── Main container ───────────────────────────────────────── */

#main {
  flex: 1;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  min-height: 0;
  background: #f2f2f7;
}

/* ── Overlay ──────────────────────────────────────────────── */

#overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 1100;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}

#overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ── Sidebar ─────────────────────────────────────────────── */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  background: #f2f2f7;
  border-right: 1px solid rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  z-index: 1200;
  transition: transform var(--transition), box-shadow var(--transition);
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.1) transparent;
}

#sidebar::-webkit-scrollbar {
  width: 4px;
}

#sidebar::-webkit-scrollbar-track {
  background: transparent;
}

#sidebar::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 99px;
}

/* ── Header ──────────────────────────────────────────────── */

header {
  padding: 18px 16px 18px;
  background: linear-gradient(160deg, #004AC2 0%, #0071E3 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
  border-radius: 16px;
  margin: 8px 8px 2px 8px;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 9px;
  color: #fff;
}

.header-title svg {
  flex-shrink: 0;
  opacity: 0.9;
}

header h1 {
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  letter-spacing: -0.03em;
  line-height: 1.2;
}

.subtitle {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.52);
  letter-spacing: 0.01em;
}

#close-btn {
  display: none;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 9px;
  color: #fff;
  cursor: pointer;
  padding: 7px;
  line-height: 0;
  transition: background var(--transition);
}

#close-btn:hover {
  background: rgba(255, 255, 255, 0.24);
}

.file-labels {
  display: flex;
  gap: 8px;
}

.file-label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  letter-spacing: -0.01em;
  transition: background var(--transition), border-color var(--transition);
  user-select: none;
}

.file-label:hover {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.35);
}

.file-label:active {
  background: rgba(255, 255, 255, 0.28);
}

.file-label svg {
  flex-shrink: 0;
  opacity: 0.8;
}

#file-input,
#intersection-file-input {
  display: none;
}

/* ── Sections ────────────────────────────────────────────── */

#legend, #piste-section, #intersection-section {
  margin: 6px 8px;
  padding: 12px 12px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05), 0 0 0 0.5px rgba(0,0,0,0.06);
}

#legend {
  margin-top: 10px;
}

#legend h2, #piste-section h2, #intersection-section h2 {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

#piste-count, #intersection-count {
  font-weight: 400;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
}

/* ── Legend items ─────────────────────────────────────────── */

#legend-items {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend-line-wrap {
  flex-shrink: 0;
  width: 36px;
  height: 16px;
  display: flex;
  align-items: center;
}

.legend-line {
  width: 36px;
  border-radius: 99px;
}

.legend-label {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.4;
  letter-spacing: -0.01em;
}

.legend-arrow {
  margin-left: 3px;
  font-size: 11px;
  color: var(--text-muted);
}

.legend-separator {
  height: 1px;
  background: var(--border);
  margin: 3px 0;
}

.legend-circle-wrap {
  flex-shrink: 0;
  width: 36px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.legend-circle {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  outline-offset: 1px;
}

/* ── Piste list ───────────────────────────────────────────── */

#piste-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

#piste-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

#piste-list li:hover {
  background: var(--bg-hover);
}

#piste-list li:active {
  background: rgba(0, 0, 0, 0.06);
}

#piste-list li.active {
  background: var(--accent-light);
}

#piste-list li.active .piste-name {
  color: var(--accent);
  font-weight: 500;
}

.piste-dot {
  flex-shrink: 0;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px rgba(0, 0, 0, 0.1);
}

.piste-name {
  flex: 1;
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

.piste-type-badge {
  flex-shrink: 0;
  font-size: 10.5px;
  font-weight: 500;
  color: var(--text-secondary);
  background: rgba(0, 0, 0, 0.05);
  border-radius: 99px;
  padding: 2px 8px;
  letter-spacing: -0.01em;
}

.intersection-badge {
  color: #ff3b30;
  background: rgba(255, 59, 48, 0.1);
}

#intersection-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

#intersection-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

#intersection-list li:hover {
  background: rgba(255, 59, 48, 0.05);
}

#intersection-list li:active {
  background: rgba(255, 59, 48, 0.08);
}

#intersection-section {
  margin-bottom: 10px;
}

/* ── Map ──────────────────────────────────────────────────── */

#map {
  flex: 1;
  min-height: 0;
  position: relative;
  border-radius: 14px;
  overflow: hidden;
  margin: 8px 8px 8px 0;
  box-shadow: var(--shadow-sm);
}

.leaflet-tile-pane {
  filter: grayscale(50%) brightness(1.06) saturate(0.65);
}

/* ── Toggle button ────────────────────────────────────────── */

#sidebar-toggle {
  position: absolute;
  top: 14px;
  left: 14px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  padding: 10px 11px;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  color: var(--text-primary);
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 0;
  transition: box-shadow var(--transition), transform 0.15s;
  -webkit-tap-highlight-color: transparent;
}

#sidebar-toggle:active {
  transform: scale(0.93);
  box-shadow: var(--shadow-sm);
}

/* ── Empty state ──────────────────────────────────────────── */

.empty-hint {
  color: var(--text-muted);
  font-size: 12px;
  font-style: italic;
  padding: 4px 10px;
  letter-spacing: -0.01em;
}

/* ── Leaflet popup ────────────────────────────────────────── */

.leaflet-popup-content-wrapper {
  border-radius: 16px !important;
  box-shadow: 0 20px 60px rgba(0,0,0,0.12), 0 4px 16px rgba(0,0,0,0.07) !important;
  border: 1px solid rgba(0, 0, 0, 0.06) !important;
  padding: 0 !important;
  background: rgba(255, 255, 255, 0.9) !important;
  backdrop-filter: blur(20px) saturate(180%) !important;
  -webkit-backdrop-filter: blur(20px) saturate(180%) !important;
}

.leaflet-popup-content {
  margin: 14px 18px !important;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif !important;
  font-size: 13px !important;
  line-height: 1.5 !important;
  color: var(--text-primary) !important;
  letter-spacing: -0.01em !important;
}

.leaflet-popup-tip-container {
  margin-top: -1px;
}

.leaflet-popup-tip {
  background: rgba(255, 255, 255, 0.9) !important;
}

/* ── Leaflet zoom controls ────────────────────────────────── */

.leaflet-bar {
  border-radius: 12px !important;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.08) !important;
  box-shadow: var(--shadow-md) !important;
}

.leaflet-bar a {
  background: rgba(255, 255, 255, 0.85) !important;
  backdrop-filter: blur(16px) !important;
  -webkit-backdrop-filter: blur(16px) !important;
  color: var(--text-primary) !important;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06) !important;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif !important;
  font-weight: 400 !important;
}

.leaflet-bar a:last-child {
  border-bottom: none !important;
}

.leaflet-bar a:hover {
  background: rgba(255, 255, 255, 0.98) !important;
}

/* ── Mobile ───────────────────────────────────────────────── */

@media (max-width: 767px) {
  #overlay {
    display: block;
    pointer-events: none;
  }

  #sidebar {
    position: fixed;
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    left: 0;
    height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top));
    width: min(var(--sidebar-width), 88vw);
    min-width: unset;
    transform: translateX(-100%);
    box-shadow: none;
  }

  #sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  #close-btn {
    display: flex;
  }

  #main {
    flex: 1;
    min-height: 0;
  }

  #map {
    width: 100%;
    flex: 1;
    border-radius: 0;
    margin: 0;
    box-shadow: none;
  }

  #sidebar-toggle {
    display: flex;
  }

  .leaflet-top.leaflet-left {
    left: auto;
    right: 0;
  }

  #piste-list li {
    padding: 11px 10px;
  }

  .piste-type-badge {
    font-size: 10.5px;
    padding: 2px 8px;
  }
}
