/* ── Variables ──────────────────────────────────────────────────────────── */
:root {
  --header-h: 52px;
  --sidebar-w: 300px;
  --panel-w: 360px;

  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-raised: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --text-primary: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;

  --accent: #2563eb;
  --accent-hover: #1d4ed8;

  --radius: 8px;
  --radius-sm: 4px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px rgba(0,0,0,.1), 0 4px 6px rgba(0,0,0,.05);

  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, monospace;

  --transition: 0.22s ease;
}

/* ── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}

input, select {
  font-family: inherit;
  font-size: 13px;
}

h2 { margin: 0; }
p  { margin: 0; }
pre { margin: 0; }
code { font-family: var(--mono); }

/* ── App shell ──────────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
#header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 200;
  gap: 12px;
}

#header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

#sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}
#sidebar-toggle:hover {
  background: var(--bg);
  color: var(--text-primary);
}

#app-title {
  display: flex;
  align-items: center;
  gap: 7px;
  font-weight: 600;
  font-size: 15px;
  color: var(--text-primary);
  white-space: nowrap;
}
#app-title svg { color: var(--accent); flex-shrink: 0; }

#route-count {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  background: var(--bg);
  padding: 3px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
}

/* ── Main layout ────────────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width var(--transition);
  z-index: 100;
}

#sidebar.collapsed {
  width: 0;
  border-right-width: 0;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 14px 16px 8px;
}

/* ── Filters ────────────────────────────────────────────────────────────── */
#filter-section {
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
}

.filter-row {
  padding: 4px 14px;
}

.filter-label {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.input-pair {
  display: flex;
  align-items: center;
  gap: 4px;
}

.input-pair input,
.filter-row select {
  flex: 1;
  min-width: 0;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text-primary);
  transition: border-color var(--transition);
}
.input-pair input:focus,
.filter-row select:focus {
  outline: none;
  border-color: var(--accent);
}

.sep {
  color: var(--text-muted);
  font-size: 12px;
  flex-shrink: 0;
}

.filter-row select {
  width: 100%;
}

.btn-link {
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  padding: 2px 4px;
  border-radius: var(--radius-sm);
  letter-spacing: 0;
  text-transform: none;
  transition: background var(--transition);
}
.btn-link:hover { background: #eff6ff; }

/* ── Route list ─────────────────────────────────────────────────────────── */
#route-list-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#route-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0 8px;
}

.route-item {
  display: flex;
  align-items: stretch;
  gap: 0;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background var(--transition), border-color var(--transition);
  position: relative;
}
.route-item:hover   { background: var(--surface-raised); }
.route-item.active  { background: #eff6ff; border-left-color: var(--accent); }

.route-color-bar {
  width: 4px;
  flex-shrink: 0;
  border-radius: 0 2px 2px 0;
  margin: 8px 0;
}

.route-item-body {
  flex: 1;
  min-width: 0;
  padding: 10px 14px 10px 10px;
}

.route-item-name {
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-primary);
  margin-bottom: 3px;
}

.route-item-meta {
  display: flex;
  gap: 10px;
  font-size: 11px;
  color: var(--text-secondary);
}

.no-routes {
  padding: 24px 16px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

/* ── Map container ──────────────────────────────────────────────────────── */
#map-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
}

/* ── Map warnings badge ─────────────────────────────────────────────────── */
#map-warnings {
  position: absolute;
  bottom: 28px;
  left: 10px;
  z-index: 500;
}

.warning-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fef9c3;
  border: 1px solid #fde68a;
  color: #92400e;
  font-size: 12px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 20px;
  cursor: help;
  box-shadow: var(--shadow-sm);
}

/* ── Empty / error states ───────────────────────────────────────────────── */
#empty-state,
#error-state {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.hidden { display: none !important; }

.state-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 48px;
  text-align: center;
  max-width: 400px;
  box-shadow: var(--shadow-md);
}

.state-icon {
  font-size: 40px;
  margin-bottom: 16px;
}

.state-box h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
}

.state-box p {
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 12px;
}

.state-box pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-primary);
}

/* ── Detail panel ───────────────────────────────────────────────────────── */
#detail-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--panel-w);
  z-index: 400;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  transition: transform var(--transition);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#detail-panel.open {
  transform: translateX(0);
}

#panel-inner {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

#panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

#panel-name {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  color: var(--text-primary);
}

#close-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  flex-shrink: 0;
  margin-top: -2px;
  transition: background var(--transition), color var(--transition);
}
#close-panel:hover {
  background: var(--bg);
  color: var(--text-primary);
}

/* ── Panel meta grid ────────────────────────────────────────────────────── */
#panel-meta {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.meta-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.meta-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.meta-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.meta-value {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
}

/* ── Highlight toggle ───────────────────────────────────────────────────── */
#panel-actions {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  user-select: none;
}

.toggle-label input[type="checkbox"] { display: none; }

.toggle-track {
  display: inline-block;
  width: 34px;
  height: 18px;
  border-radius: 9px;
  background: var(--border-strong);
  position: relative;
  flex-shrink: 0;
  transition: background var(--transition);
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
  transition: transform var(--transition);
}
.toggle-label input:checked ~ .toggle-track {
  background: var(--accent);
}
.toggle-label input:checked ~ .toggle-track::after {
  transform: translateX(16px);
}

/* ── Elevation chart ────────────────────────────────────────────────────── */
#elevation-wrap {
  padding: 16px 20px 20px;
}

#elevation-chart-container {
  height: 160px;
  position: relative;
}

/* ── Sidebar overlay (mobile tap-to-close) ──────────────────────────────── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.3);
  z-index: 90;
}

/* ── Leaflet tooltip tweak ──────────────────────────────────────────────── */
.leaflet-tooltip {
  font-family: var(--font);
  font-size: 12px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

/* ── Scrollbar ──────────────────────────────────────────────────────────── */
#route-list::-webkit-scrollbar,
#panel-inner::-webkit-scrollbar { width: 4px; }
#route-list::-webkit-scrollbar-track,
#panel-inner::-webkit-scrollbar-track { background: transparent; }
#route-list::-webkit-scrollbar-thumb,
#panel-inner::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* ── Mobile (≤ 768 px) ──────────────────────────────────────────────────── */
@media (max-width: 768px) {

  /* Sidebar becomes a fixed drawer */
  #sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: 0;
    width: min(var(--sidebar-w), 88vw);
    transform: translateX(-100%);
    transition: transform var(--transition);
    box-shadow: var(--shadow-lg);
    z-index: 300;
  }
  #sidebar.open {
    transform: translateX(0);
  }
  /* Sidebar 'collapsed' class has no effect on mobile — handled by 'open' */
  #sidebar.collapsed {
    width: min(var(--sidebar-w), 88vw);
    border-right-width: 1px;
  }

  #sidebar-overlay.active {
    display: block;
  }

  /* Detail panel becomes a bottom sheet */
  #detail-panel {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 58vh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    transform: translateY(100%);
  }
  #detail-panel.open {
    transform: translateY(0);
  }

  /* Drag handle hint */
  #panel-inner::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--border-strong);
    border-radius: 2px;
    margin: 10px auto 0;
  }

  .meta-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
}

@media (max-width: 480px) {
  .meta-grid { grid-template-columns: 1fr 1fr; }
  #elevation-chart-container { height: 130px; }
}
