::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: #1e293b; }
::-webkit-scrollbar-thumb { background: #475569; border-radius: 3px; }
* { scrollbar-width: thin; scrollbar-color: #475569 #1e293b; }

/* Remove the 300ms tap delay on touch devices; keep native double-tap
   zoom on the map itself since MapLibre relies on it. */
button, a, select, input, .layer-pill, .nav-main-btn { touch-action: manipulation; }

/* Visible keyboard focus everywhere, without adding a ring to mouse/touch
   clicks (Tailwind's own focus:ring utilities still apply where set). */
:focus-visible { outline: 2px solid #38bdf8; outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

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

/* Hidden on screen, shown only inside @media print below. */
.print-only { display: none; }

/* ══════════════════════════════════════════════════════════════
   PRINTING THE TERRITORY PANEL (#territory-modal)
   Goal: print the panel's current tab exactly as it looks on screen
   (same dark theme, same colors, same multi-column layout) — just
   isolated from the rest of the app, and flowing across as many
   pages as it needs instead of being clipped to the on-screen
   scrollable viewport.
   ══════════════════════════════════════════════════════════════ */
@media print {
  @page { margin: 12mm; }

  /* Keep the dark cards / colored badges / chart fills intact on paper —
     browsers routinely skip background colors/images by default when
     printing unless told otherwise. */
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }

  /* Isolate the panel — hide everything else on the page */
  html, body { background: #fff !important; }
  body * { visibility: hidden; }
  #territory-modal, #territory-modal * { visibility: visible; }

  /* The panel/body wrapper use near-black slate-900/950 fills on screen —
     fine behind light text on a monitor, but printed as solid ink across
     every page it's wasteful and looks wrong on paper. */
  #territory-modal, #tm-panel {
    background: #fff !important;
  }

  /* Content cards use a *translucent* dark fill (bg-slate-800/40, /60…)
     designed to blend with the dark panel behind them. Now that the panel
     is white, that translucency would wash them out to a low-contrast gray
     and their colored text (pink/violet/amber/teal) would lose legibility.
     Force them to the same *solid* dark tone the on-screen version blends
     down to, so every card still looks/reads exactly as it does on screen.
     Scoped to #tm-content (not #territory-modal) so it doesn't also catch
     — and override — #tm-panel's own bg-slate-900 class. */
  #tm-content [class*="bg-slate-8"],
  #tm-content [class*="bg-slate-9"] {
    background-color: #1e293b !important;
  }

  #territory-modal {
    position: static !important;
    inset: auto !important;
    transform: none !important;
    width: 100% !important;
    height: auto !important;
    padding: 0 !important;
    display: block !important;
  }

  #tm-backdrop, .no-print { display: none !important; }
  .print-only { display: block !important; }

  #tm-panel {
    position: static !important;
    width: 100% !important;
    max-width: none !important;
    height: auto !important;
    max-height: none !important;
    border: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
  }

  #tm-tabs { display: none !important; }

  /* Let the full tab content flow across as many pages as it needs,
     instead of being clipped to the on-screen scrollable viewport. */
  #tm-content {
    overflow: visible !important;
    height: auto !important;
    max-height: none !important;
  }

  /* Never split a card/table/chart across a page break */
  #territory-modal [class*="rounded-xl"],
  #territory-modal [class*="rounded-lg"],
  #territory-modal table,
  #territory-modal tr,
  #territory-modal .kpi-grid > *,
  #territory-modal .chart-grid-1 > *,
  #territory-modal .chart-grid-2 > *,
  #territory-modal .chart-grid-3 > * {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  #territory-modal canvas { max-width: 100%; }
}

/* Hide the native <summary> disclosure triangle (list-none handles Chrome/
   Firefox; Safari needs this explicitly) — used by every accordion. */
summary::-webkit-details-marker { display: none; }

.maplibregl-popup-content {
  background: #1e293b; color: #cbd5e1;
  border: 1px solid #334155; border-radius: 8px;
  padding: 10px 14px; max-width: 320px; font-size: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,.5);
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip { border-top-color: #1e293b; }
.maplibregl-popup-anchor-top    .maplibregl-popup-tip { border-bottom-color: #1e293b; }
.maplibregl-popup-anchor-left   .maplibregl-popup-tip { border-right-color: #1e293b; }
.maplibregl-popup-anchor-right  .maplibregl-popup-tip { border-left-color: #1e293b; }
.maplibregl-popup-close-button { color: #cbd5e1; font-size: 16px; padding: 2px 6px; }
.maplibregl-popup-close-button:hover { color: #cbd5e1; background: transparent; }
.maplibregl-ctrl-attrib { background: rgba(15,23,42,.75) !important; color: #cbd5e1 !important; }
.maplibregl-ctrl-attrib a { color: #38bdf8 !important; }

.popup-title { font-weight: 700; font-size: 13px; color: #38bdf8; margin-bottom: 6px; }
.popup-row { display: flex; justify-content: space-between; gap: 12px; line-height: 1.6; }
.popup-key { color: #cbd5e1; }
.popup-val { color: #cbd5e1; font-weight: 500; }

.layer-pill {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 9999px; cursor: pointer;
  font-size: 11px; font-weight: 600; letter-spacing: .3px;
  transition: opacity .15s, transform .1s; user-select: none;
}
.layer-pill:hover { transform: scale(1.04); }
.layer-pill.active   { opacity: 1; }
.layer-pill.inactive { opacity: .4; }
.layer-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

.stat-card {
  background: #1e293b; border: 1px solid #334155;
  border-radius: 10px; padding: 12px 16px;
}
#profile-table td, #profile-table th {
  padding: 4px 8px; font-size: 11px;
  border-bottom: 1px solid #334155;
}
#profile-table th { color: #cbd5e1; text-align: left; }
#profile-table td { color: #cbd5e1; }
/* ── Responsive drawers (< 1024 px) ─────────────────────── */
@media (max-width: 1023px) {
  #left-sidebar {
    position: fixed;
    left: 0; top: 44px; bottom: 0;
    z-index: 301;
    /* Wider than the static desktop rail — a 224px drawer feels cramped
       for a touch-driven overlay; cap it so it never eats the whole screen
       on very small phones. */
    width: min(84vw, 320px);
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
  }
  #left-sidebar.drawer-open {
    transform: translateX(0);
    box-shadow: 4px 0 32px rgba(0,0,0,.7);
  }
}

/* ── Touch-friendly tap targets (< 1024 px) ───────────────
   Desktop keeps its dense, mouse-driven sizing; below the drawer
   breakpoint every primary tap target grows toward the 44px minimum
   and gets a bit more breathing room so fingers don't mis-tap. ──── */
@media (max-width: 1023px) {
  .layer-pill { padding: 8px 12px; font-size: 12px; }
  #layer-list .space-y-1 > * + * { margin-top: 6px; }
  .nav-main-btn { padding-top: 8px; padding-bottom: 8px; }
}

/* ── Document modal form helpers ─────────────────────────── */
.doc-label {
  display: block; font-size: 10px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; color: #cbd5e1;
}
.doc-input {
  width: 100%; background: #1e293b; border: 1px solid #475569;
  border-radius: 8px; padding: 8px 12px; font-size: 12px;
  color: #cbd5e1; outline: none; transition: border-color .15s;
}
.doc-input:focus { border-color: #38bdf8; box-shadow: 0 0 0 1px #38bdf888; }
.line-clamp-1 {
  overflow: hidden; display: -webkit-box;
  -webkit-box-orient: vertical; -webkit-line-clamp: 1;
}

/* ── Flex-based responsive card rows for territory-modal / thematic tab
   content. Cards wrap and grow to fill their row based on the *actual*
   available width (side panel, full-width mobile sheet, etc.) instead of
   the viewport, so KPI/chart cards never overflow or get crushed — and
   never force their row wider than the panel. ─────────────────────────── */
.kpi-grid, .chart-grid-1, .chart-grid-2, .chart-grid-3 {
  display: flex;
  flex-wrap: wrap;
  min-width: 0;
}
.kpi-grid      { gap: 0.625rem; }
.chart-grid-1  { gap: 1rem; flex-direction: column; }
.chart-grid-2  { gap: 1rem; }
.chart-grid-3  { gap: 1rem; }
.kpi-grid      > * { flex: 1 1 112px; min-width: 0; }
.chart-grid-1  > * { flex: 1 1 auto; min-width: 0; }
.chart-grid-2  > * { flex: 1 1 220px; min-width: 0; }
.chart-grid-3  > * { flex: 1 1 180px; min-width: 0; }

/* ── Container-aware fluid type ──────────────────────────────────────────
   #tm-content is sized-contained so descendant text can scale off the
   panel's real inline size (440px desktop rail vs full-width mobile sheet)
   instead of the viewport — a fixed rem value is kept first as a fallback
   for browsers without container-query unit support. ───────────────────── */
#tm-content { container-type: inline-size; container-name: tmc; }

.tm-figure {
  font-size: 1.25rem;
  font-size: clamp(1rem, 4.5cqi, 1.375rem);
  line-height: 1.2;
}
.tm-figure-sm {
  font-size: 0.8rem;
  font-size: clamp(0.75rem, 3cqi, 0.95rem);
  line-height: 1.25;
}
.tm-figure-unit {
  font-size: 0.75rem;
  font-size: clamp(0.6875rem, 2.4cqi, 0.8125rem);
}

/* ── Territory Panel — right-side slide-in on lg screens ─── */
@media (min-width: 1024px) {
  #territory-modal {
    /* Override the centered-modal positioning */
    position: fixed;
    inset: auto;
    right: 0;
    top: 44px;
    bottom: 0;
    width: 440px;
    /* Override flex centering */
    align-items: stretch;
    justify-content: stretch;
    padding: 0;
    /* Off-screen by default — slide in when .tm-open is added */
    transform: translateX(100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
  }
  #territory-modal.tm-open {
    transform: translateX(0);
    pointer-events: auto;
  }
  #tm-panel {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border-right: none;
    border-top: none;
    border-bottom: none;
    box-shadow: -8px 0 40px rgba(0, 0, 0, 0.55);
  }
}

/* ── Territory Panel — fade + slide-up sheet on < lg ──────
   Mirrors the lg slide-in-from-right transition above so the mobile
   bottom sheet doesn't just snap open/closed. ────────────────────── */
@media (max-width: 1023px) {
  #tm-backdrop { opacity: 0; transition: opacity .25s ease; }
  #tm-panel {
    transform: translateY(16px);
    opacity: 0;
    transition: transform .28s cubic-bezier(.4,0,.2,1), opacity .22s ease;
  }
  #territory-modal.tm-open #tm-backdrop { opacity: 1; }
  #territory-modal.tm-open #tm-panel    { transform: translateY(0); opacity: 1; }
}

/* ── Document modals — quick fade + scale-in ──────────────
   Toggled via the existing hidden/flex swap plus a .modal-open class
   added one frame later (see documentsPanel.js). ────────────────── */
#doc-modal, #doc-viewer-modal {
  opacity: 0;
  transition: opacity .18s ease;
}
#doc-modal.modal-open, #doc-viewer-modal.modal-open { opacity: 1; }
#doc-modal > div, #doc-viewer-modal > div {
  transform: scale(.97) translateY(6px);
  transition: transform .18s cubic-bezier(.4,0,.2,1);
}
#doc-modal.modal-open > div, #doc-viewer-modal.modal-open > div {
  transform: scale(1) translateY(0);
}

/* Backdrop overlay */
#drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 299;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
#drawer-overlay.active { display: block; }
