:root {
  --bg: #12161c;
  --panel: #1a2029;
  --panel-2: #222a36;
  --text: #e8ecf1;
  --text-dim: #9aa7b5;
  --accent: #f5a623;
  --accent-dark: #c9820f;
  --line: #2e3846;
  --mountain: #a78bfa;
  --coastal: #38bdf8;
  --touring: #4ade80;
  --technical: #f87171;
  --radius: 12px;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-icon { font-size: 22px; }
.brand h1 { font-size: 18px; font-weight: 700; letter-spacing: 0.3px; }

.header-actions { display: flex; gap: 8px; align-items: center; }

.btn-trip {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn-trip.open { border-color: var(--accent); color: var(--accent); }

.trip-count {
  background: var(--accent);
  color: #1a1408;
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 11px;
}
.trip-count.hidden { display: none; }

.btn-near-me {
  background: var(--accent);
  color: #1a1408;
  border: none;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-near-me:active { background: var(--accent-dark); }
.btn-near-me.busy { opacity: 0.6; pointer-events: none; }

/* ---------- Filters ---------- */
.filter-bar {
  padding: 8px 16px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}

#search {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}
#search::placeholder { color: var(--text-dim); }
#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.chip-row { display: flex; gap: 6px; flex-wrap: wrap; }

.chip {
  background: var(--panel-2);
  color: var(--text-dim);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1408;
  font-weight: 600;
}

/* ---------- Layout ---------- */
.layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(320px, 400px) 1fr;
  min-height: 0;
}

#map {
  grid-column: 2;
  grid-row: 1;
  height: 100%;
  background: #0b0e12;
}

.route-list-panel {
  grid-column: 1;
  grid-row: 1;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  font-size: 12.5px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
.fav-filter { display: flex; align-items: center; gap: 5px; cursor: pointer; }

#route-list { list-style: none; }

.route-card {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.route-card:hover { background: var(--panel); }
.route-card.selected { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }

.route-card .top-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.route-card h3 { font-size: 15px; font-weight: 600; }
.route-card .road { color: var(--text-dim); font-size: 12px; white-space: nowrap; }

.route-card .meta {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 5px;
  font-size: 12px;
  color: var(--text-dim);
  flex-wrap: wrap;
}

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge.mountain  { background: rgba(167,139,250,0.15); color: var(--mountain); }
.badge.coastal   { background: rgba(56,189,248,0.15);  color: var(--coastal); }
.badge.touring   { background: rgba(74,222,128,0.15);  color: var(--touring); }
.badge.technical { background: rgba(248,113,113,0.15); color: var(--technical); }

.flags { margin-left: auto; font-size: 13px; }
.distance-tag { color: var(--accent); font-weight: 600; }

.empty-msg { padding: 24px 16px; color: var(--text-dim); font-size: 14px; text-align: center; }

/* ---------- Detail panel ---------- */
.detail-panel {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 380px;
  max-width: calc(100vw - 32px);
  max-height: 70vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0,0,0,0.55);
  padding: 18px;
  z-index: 1000;
}
.detail-panel.hidden { display: none; }

.detail-close {
  position: absolute;
  top: 10px;
  right: 12px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
}
.detail-close:hover { color: var(--text); }

.detail-panel h2 { font-size: 19px; margin-bottom: 2px; padding-right: 24px; }
.detail-panel .sub { color: var(--text-dim); font-size: 13px; margin-bottom: 10px; }

.stat-row {
  display: flex;
  gap: 14px;
  margin: 10px 0;
  font-size: 13px;
  flex-wrap: wrap;
}
.stat-row .stat b { display: block; font-size: 15px; color: var(--text); }
.stat-row .stat span { color: var(--text-dim); font-size: 11.5px; }

.seasonal-warning {
  background: rgba(248,113,113,0.12);
  color: #fca5a5;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  margin: 8px 0;
}

.detail-panel p.desc { font-size: 13.5px; line-height: 1.55; color: var(--text); margin: 8px 0; }

.highlights { margin: 8px 0 12px; padding-left: 18px; font-size: 13px; color: var(--text-dim); }
.highlights li { margin: 3px 0; }

.action-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn.primary { background: var(--accent); color: #1a1408; }
.btn.primary:active { background: var(--accent-dark); }
.btn.ghost {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
}
.btn.ghost.on { border-color: var(--accent); color: var(--accent); }

.source-link { font-size: 12px; color: var(--text-dim); margin-top: 12px; display: block; }
.source-link a { color: var(--coastal); }

/* ---------- Trip panel ---------- */
.trip-panel {
  position: fixed;
  left: 16px;
  bottom: 16px;
  width: 400px;
  max-width: calc(100vw - 32px);
  max-height: 72vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0,0,0,0.55);
  padding: 18px;
  z-index: 1001;
}
.trip-panel.hidden { display: none; }
.trip-panel h2 { font-size: 18px; margin-bottom: 4px; }
.trip-hint { font-size: 12.5px; color: var(--text-dim); margin-bottom: 10px; line-height: 1.45; }

#trip-legs { list-style: none; margin: 0 0 10px; }

.trip-leg {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  margin-bottom: 4px;
}
.trip-leg .leg-top {
  display: flex;
  align-items: center;
  gap: 8px;
}
.trip-leg .leg-num {
  background: var(--accent);
  color: #1a1408;
  border-radius: 999px;
  min-width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex: 0 0 auto;
}
.trip-leg .leg-name { font-size: 13.5px; font-weight: 600; flex: 1 1 auto; }
.trip-leg .leg-meta { font-size: 11.5px; color: var(--text-dim); margin: 3px 0 0 28px; }
.trip-leg .leg-actions { display: flex; gap: 4px; flex: 0 0 auto; }
.trip-leg .leg-actions button,
.trip-leg .leg-actions a {
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 12px;
  padding: 3px 7px;
  cursor: pointer;
  text-decoration: none;
}
.trip-leg .leg-actions button:hover, .trip-leg .leg-actions a:hover { color: var(--text); }
.trip-leg .leg-actions button:disabled { opacity: 0.3; cursor: default; }

.transit-leg {
  font-size: 11.5px;
  color: var(--text-dim);
  padding: 3px 0 7px 28px;
  border-left: 2px dashed var(--line);
  margin-left: 10px;
  margin-bottom: 4px;
}
.transit-leg a { color: var(--coastal); text-decoration: none; }

.trip-review {
  background: rgba(245, 166, 35, 0.08);
  border: 1px solid rgba(245, 166, 35, 0.35);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12.5px;
  line-height: 1.5;
  margin-bottom: 10px;
  color: var(--text);
}
.trip-review.hidden { display: none; }
.trip-review b { color: var(--accent); }

.loop-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text-dim);
  cursor: pointer;
  margin: 2px 0 10px;
}

.suggest-road {
  padding: 12px 14px 16px;
  font-size: 12.5px;
  color: var(--text-dim);
  border-top: 1px solid var(--line);
}
.suggest-road a { color: var(--accent); text-decoration: none; font-weight: 600; }

.trip-totals {
  font-size: 13px;
  background: var(--panel-2);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 4px;
}
.trip-totals b { color: var(--accent); }
.trip-empty { color: var(--text-dim); font-size: 13px; padding: 10px 0 14px; }

#trip-name {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}
#trip-name::placeholder { color: var(--text-dim); font-weight: 400; }
#trip-name:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- Print run sheet ---------- */
#print-sheet { display: none; }

@media print {
  html, body { height: auto; overflow: visible; background: #fff; }
  body > *:not(#print-sheet) { display: none !important; }
  #print-sheet {
    display: block;
    color: #000;
    background: #fff;
    font-family: Georgia, "Times New Roman", serif;
    padding: 0.5cm;
  }
  #print-sheet h1 { font-size: 22pt; margin-bottom: 8pt; border-bottom: 2pt solid #000; padding-bottom: 4pt; }
  #print-sheet .ps-review { font-size: 11pt; margin: 6pt 0 8pt; font-style: italic; }
  #print-sheet .ps-fill { font-size: 11pt; margin: 6pt 0; }
  #print-sheet .ps-leg { margin: 10pt 0 4pt; page-break-inside: avoid; }
  #print-sheet .ps-leg h3 { font-size: 13pt; margin-bottom: 2pt; }
  #print-sheet .ps-leg h3 span { font-weight: normal; color: #444; }
  #print-sheet .ps-meta { font-size: 10.5pt; }
  #print-sheet .ps-warn { font-size: 10.5pt; font-weight: bold; }
  #print-sheet .ps-notes { font-size: 10pt; color: #333; font-style: italic; }
  #print-sheet .ps-transit { font-size: 9.5pt; color: #555; margin: 4pt 0 4pt 12pt; }
  #print-sheet .ps-total { margin-top: 10pt; font-size: 11pt; border-top: 1pt solid #000; padding-top: 6pt; }
  #print-sheet .ps-link { font-size: 9pt; color: #333; word-break: break-all; margin-top: 6pt; }
}

/* ---------- Leaflet dark-ish tweaks ---------- */
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--panel);
  color: var(--text);
}
.leaflet-popup-content { font-size: 13px; }
.leaflet-popup-content b { color: var(--accent); }

/* ---------- Mobile ---------- */
@media (max-width: 800px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 44vh 1fr;
  }
  #map { grid-column: 1; grid-row: 1; }
  .route-list-panel {
    grid-column: 1;
    grid-row: 2;
    border-right: none;
    border-top: 1px solid var(--line);
  }
  .detail-panel,
  .trip-panel {
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 75vh;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
  .brand h1 { font-size: 16px; }
  .btn-near-me, .btn-trip { padding: 7px 10px; font-size: 12px; }
}
