@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

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

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  color: #0f172a;
  background: #f1f5f9;
}

#app { display: flex; height: 100%; }

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

#sidebar {
  width: 300px;
  flex-shrink: 0;
  background: #f8fafc;
  border-right: 1px solid #e2e8f0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* Header */
.sidebar-header {
  padding: 14px 16px 12px;
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 10px;
}

.brand-logo { height: 20px; width: auto; display: block; }

.brand-divider {
  width: 1px; height: 16px;
  background: #e2e8f0;
  flex-shrink: 0;
}

.brand-mode-badge {
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: #2563eb;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 4px;
  padding: 2px 6px;
  text-transform: uppercase;
  white-space: nowrap;
}

.product-title {
  font-size: 14px;
  font-weight: 600;
  color: #0f172a;
  letter-spacing: -0.01em;
  margin-bottom: 1px;
}

.product-subtitle {
  font-size: 11px;
  color: #94a3b8;
}

/* Sections */
.sidebar-section {
  padding: 12px 14px;
  border-bottom: 1px solid #e2e8f0;
  background: #ffffff;
  margin-top: 6px;
}

.sidebar-section:first-of-type { margin-top: 6px; }

.section-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: #94a3b8;
  margin-bottom: 8px;
}

/* ─── Market pills ──────────────────────────────────────────────────────────── */

.market-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.market-pill {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  padding: 4px 11px;
  font-size: 11px;
  font-weight: 500;
  color: #475569;
  cursor: pointer;
  transition: all 0.12s;
  font-family: inherit;
  white-space: nowrap;
}

.market-pill:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #1e3a5f;
}

.market-pill.active {
  background: #1e3a5f;
  border-color: #1e3a5f;
  color: #ffffff;
  font-weight: 600;
}

.market-summary {
  font-size: 11px;
  color: #94a3b8;
  margin-top: 8px;
  line-height: 1.4;
}

/* ─── Scenario cards ────────────────────────────────────────────────────────── */

.scenario-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.scenario-card {
  border: 1.5px solid #e2e8f0;
  border-radius: 9px;
  padding: 9px 11px;
  cursor: pointer;
  transition: all 0.12s;
  background: #ffffff;
  font-family: inherit;
  text-align: left;
  width: 100%;
}

.scenario-card:hover { border-color: #94a3b8; background: #f8fafc; }

.scenario-card.active {
  border-color: #1e3a5f;
  background: #f0f4f9;
}

.scenario-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.scenario-name {
  font-size: 12px;
  font-weight: 600;
  color: #0f172a;
}

.scenario-badge {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-radius: 4px;
  padding: 2px 6px;
  text-transform: uppercase;
  white-space: nowrap;
}

.scenario-badge.recommended { background: #dcfce7; color: #166534; }
.scenario-badge.alternative  { background: #fef3c7; color: #92400e; }
.scenario-badge.future       { background: #ede9fe; color: #5b21b6; }
.scenario-badge.standard     { background: #f1f5f9; color: #475569; }

.scenario-desc {
  font-size: 10px;
  color: #64748b;
  line-height: 1.4;
}

.k-warning {
  margin-top: 7px;
  padding: 8px 10px;
  background: #fffbeb;
  border: 1px solid #fbbf24;
  border-radius: 8px;
  font-size: 10px;
  line-height: 1.5;
  color: #92400e;
}

/* ─── Territory cards ───────────────────────────────────────────────────────── */

.territory-cards { display: flex; flex-direction: column; gap: 5px; }

.territory-card {
  background: #ffffff;
  border: 1.5px solid #e2e8f0;
  border-radius: 9px;
  padding: 9px 11px;
  cursor: pointer;
  transition: all 0.12s;
}

.territory-card:hover { border-color: #94a3b8; background: #f8fafc; }

.territory-card.selected {
  border-color: #1e3a5f;
  background: #f0f4f9;
}

.tc-header {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 7px;
}

.tc-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.tc-names { flex: 1; min-width: 0; }

.tc-title {
  font-size: 12px;
  font-weight: 600;
  color: #0f172a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tc-id {
  font-size: 9px;
  color: #94a3b8;
  font-family: 'SF Mono', 'Fira Code', monospace;
  margin-top: 1px;
}

.tc-badge {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.03em;
  border-radius: 4px;
  padding: 2px 6px;
  text-transform: uppercase;
  white-space: nowrap;
  flex-shrink: 0;
}

.tc-badge.in-range  { background: #dcfce7; color: #166534; }
.tc-badge.review    { background: #fef3c7; color: #92400e; }
.tc-badge.sub-scale { background: #fee2e2; color: #991b1b; }

.tc-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.tc-stat-val {
  font-size: 13px;
  font-weight: 600;
  color: #0f172a;
  line-height: 1.1;
}

.tc-stat-lbl {
  font-size: 9px;
  color: #94a3b8;
  margin-top: 1px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Territory detail card */
#territory-detail {
  margin-top: 8px;
}

.detail-placeholder {
  font-size: 11px;
  color: #94a3b8;
  text-align: center;
  padding: 10px 0;
  line-height: 1.5;
}

.detail-card {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 9px;
  padding: 11px 12px;
}

.detail-title {
  font-size: 12px;
  font-weight: 600;
  color: #0f172a;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.detail-title-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 3px 0;
  font-size: 11px;
  border-bottom: 1px solid #f1f5f9;
}

.detail-row:last-child { border-bottom: none; }
.detail-lbl { color: #64748b; }
.detail-val { font-weight: 500; color: #0f172a; text-align: right; }

/* ─── Drive-time tool ──────────────────────────────────────────────────────── */

.btn-measure {
  width: 100%;
  background: #ffffff;
  border: 1.5px solid #e2e8f0;
  border-radius: 8px;
  padding: 8px 11px;
  font-size: 11px;
  font-weight: 500;
  color: #374151;
  cursor: pointer;
  text-align: left;
  transition: all 0.12s;
  font-family: inherit;
}

.btn-measure:hover { border-color: #94a3b8; background: #f8fafc; }

.btn-measure.active {
  background: #6d28d9;
  border-color: #6d28d9;
  color: #ffffff;
}

.measure-result {
  margin-top: 8px;
  padding: 10px 11px;
  background: #faf5ff;
  border: 1px solid #c4b5fd;
  border-radius: 8px;
  font-size: 11px;
  line-height: 1.6;
  color: #4c1d95;
}

.measure-time { display: block; font-size: 15px; font-weight: 700; color: #5b21b6; }
.measure-dist { display: block; font-size: 12px; font-weight: 600; color: #6d28d9; }
.measure-note { display: block; font-size: 10px; color: #7c3aed; opacity: 0.8; margin-top: 3px; }

/* ─── Export ────────────────────────────────────────────────────────────────── */

.export-buttons { display: flex; flex-direction: column; gap: 5px; }

.btn-export {
  width: 100%;
  background: #1e3a5f;
  border: 1.5px solid #1e3a5f;
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 12px;
  font-weight: 500;
  color: #ffffff;
  cursor: pointer;
  text-align: left;
  transition: all 0.12s;
  font-family: inherit;
  display: flex;
  align-items: center;
  gap: 7px;
}

.btn-export:hover { background: #162d4a; border-color: #162d4a; }

.export-note {
  font-size: 10px;
  color: #94a3b8;
  line-height: 1.4;
  margin-top: 4px;
}

/* ─── Floating map legend (bottom-right) ────────────────────────────────────── */

#map-legend {
  position: absolute;
  bottom: 28px;
  right: 12px;
  z-index: 900;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.09);
  min-width: 190px;
  overflow: hidden;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

#legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
  user-select: none;
}

#legend-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #64748b;
}

#legend-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: #94a3b8;
  line-height: 1;
  padding: 0 0 0 8px;
  font-family: inherit;
  transition: color 0.12s;
}

#legend-toggle:hover { color: #475569; }

#legend-body {
  padding: 9px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

#legend-body.collapsed { display: none; }

.fl-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  color: #475569;
  line-height: 1.3;
}

/* Swatches */
.fl-zone {
  display: inline-block;
  width: 13px; height: 9px;
  border-radius: 2px;
  opacity: 0.55;
  flex-shrink: 0;
}

.fl-dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: #305496;
  flex-shrink: 0;
}

.fl-center {
  display: inline-block;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #374151;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1.5px #374151;
  flex-shrink: 0;
}

.fl-flagged {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: #fff;
  border: 1.5px solid #dc2626;
  flex-shrink: 0;
}

.fl-edge {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  border: 1.5px dashed #94a3b8;
  flex-shrink: 0;
}

.fl-note {
  font-size: 9px;
  color: #94a3b8;
  margin-top: 4px;
  line-height: 1.5;
  font-style: italic;
  border-top: 1px solid #f1f5f9;
  padding-top: 6px;
}

/* Footer */
.sidebar-footer {
  padding: 10px 14px;
  font-size: 9px;
  color: #94a3b8;
  margin-top: auto;
  background: #f8fafc;
}

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

#map-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#map { width: 100%; height: 100%; }

/* ─── Stats bar ─────────────────────────────────────────────────────────────── */

#stats-bar {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 900;
  display: flex;
  gap: 6px;
  pointer-events: none;
}

.stats-card {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 9px;
  padding: 7px 13px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.07);
  min-width: 68px;
}

.stats-value {
  font-size: 15px;
  font-weight: 700;
  color: #0f172a;
  letter-spacing: -0.02em;
  line-height: 1.1;
  white-space: nowrap;
}

.stats-label {
  font-size: 9px;
  color: #94a3b8;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-top: 2px;
}

/* Zone note */
#zone-note {
  position: absolute;
  bottom: 22px;
  left: 10px;
  z-index: 900;
  background: rgba(255,255,255,0.9);
  border: 1px solid #e2e8f0;
  border-radius: 7px;
  padding: 5px 9px;
  font-size: 9px;
  color: #64748b;
  pointer-events: none;
  max-width: 340px;
  line-height: 1.4;
}

/* ─── Leaflet overrides ─────────────────────────────────────────────────────── */

.leaflet-popup-content-wrapper {
  border-radius: 10px !important;
  box-shadow: 0 4px 20px rgba(0,0,0,0.11) !important;
  border: 1px solid #e2e8f0 !important;
}

.leaflet-popup-content {
  margin: 10px 13px !important;
  font-size: 12px !important;
  line-height: 1.5 !important;
  font-family: 'Inter', -apple-system, sans-serif !important;
}

.popup-name {
  font-weight: 600;
  color: #0f172a;
  margin-bottom: 4px;
  font-size: 12px;
}

.popup-row { color: #475569; margin: 1px 0; font-size: 11px; }

.popup-flag {
  margin-top: 6px;
  padding: 3px 7px;
  border-radius: 5px;
  font-size: 10px;
  font-weight: 500;
}

.popup-flag.review { background: #fef3c7; color: #92400e; }
.popup-flag.edge   { background: #eff6ff; color: #1d4ed8; }

.leaflet-control-attribution { font-size: 9px !important; }

/* ═══════════════════════════════════════════════════════════════════════════════
   RESPONSIVE — Mobile & Tablet
   Breakpoints:
     ≤ 768px  Mobile   → sidebar becomes bottom sheet
     ≤ 480px  Small    → tighter spacing, smaller type
     769–1024 Tablet   → narrower sidebar
═══════════════════════════════════════════════════════════════════════════════ */

/* Sheet grip — hidden on desktop, shown on mobile */
.sheet-grip-wrap {
  display: none;
}

/* ─── Mobile (≤ 768px) ──────────────────────────────────────────────────────── */

@media (max-width: 768px) {

  /* ── Base layout: map fills the screen, sidebar overlays it ── */
  #app {
    flex-direction: column;
    position: fixed;
    inset: 0;
  }

  #map-container {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
  }

  /* ── Sidebar → sliding bottom sheet ── */
  #sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 75vh;
    border-right: none;
    border-top: 1px solid #e2e8f0;
    border-radius: 16px 16px 0 0;
    z-index: 1000;
    /* Collapsed: peek enough to show full header block */
    transform: translateY(calc(100% - 110px));
    transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 -6px 32px rgba(0, 0, 0, 0.13);
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  #sidebar.sheet-open {
    transform: translateY(0);
  }

  /* ── Grip pill ── */
  .sheet-grip-wrap {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0 6px;
    cursor: pointer;
    user-select: none;
    background: #ffffff;
    border-radius: 16px 16px 0 0;
    flex-shrink: 0;
    min-height: 28px;
    touch-action: manipulation;
  }

  .sheet-grip {
    width: 36px;
    height: 4px;
    background: #cbd5e1;
    border-radius: 2px;
    pointer-events: none;
  }

  /* ── Header also tappable as sheet toggle ── */
  .sidebar-header {
    cursor: pointer;
    user-select: none;
  }

  /* ── Market pills → single-row horizontal scroll ── */
  .market-pills {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .market-pills::-webkit-scrollbar { display: none; }

  /* ── Scenario cards → horizontal scroll row ── */
  .scenario-list {
    flex-direction: row;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
    gap: 7px;
  }
  .scenario-list::-webkit-scrollbar { display: none; }

  .scenario-card {
    min-width: 155px;
    flex-shrink: 0;
  }

  /* ── Territory cards — stack fine, just ensure readable touch targets ── */
  .territory-card {
    padding: 10px 12px;
    min-height: 48px;
  }

  /* ── Stats bar — horizontal scroll, compact ── */
  #stats-bar {
    top: 8px;
    left: 8px;
    right: 8px;
    overflow-x: auto;
    scrollbar-width: none;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    gap: 5px;
  }
  #stats-bar::-webkit-scrollbar { display: none; }

  .stats-card {
    flex-shrink: 0;
    padding: 5px 10px;
    min-width: 58px;
  }
  .stats-value { font-size: 13px; }
  .stats-label { font-size: 8px; }

  /* ── Legend — lift above the peeking sheet ── */
  #map-legend {
    bottom: 120px;
    right: 10px;
    min-width: 165px;
  }

  /* Legend body collapse handled by JS on load */

  /* ── Leaflet touch passthrough ── */
  #map { touch-action: pan-x pan-y; }

  /* ── Popup text slightly larger for touch readability ── */
  .leaflet-popup-content { font-size: 13px !important; }

  /* ── Section spacing ── */
  .sidebar-section { margin-top: 4px; }

  /* ── Hide data stamp on mobile ── */
  .sidebar-footer { display: none; }
}

/* ─── Small phones (≤ 480px) ───────────────────────────────────────────────── */

@media (max-width: 480px) {
  .sidebar-header { padding: 10px 12px; }
  .product-title  { font-size: 13px; }
  .product-subtitle { font-size: 10px; }
  .brand-logo     { height: 17px; }

  .stats-card   { padding: 4px 8px; min-width: 52px; }
  .stats-value  { font-size: 12px; }

  #map-legend   { min-width: 148px; }
  .fl-row       { font-size: 10px; }

  .sidebar-section { padding: 10px 12px; }
}

/* ─── Tablet (769 – 1024px) ─────────────────────────────────────────────────── */

@media (min-width: 769px) and (max-width: 1024px) {
  #sidebar    { width: 260px; }
  .stats-value { font-size: 13px; }
  .stats-card  { padding: 6px 10px; min-width: 60px; }
}
