/* ══════════════════════════════════════════════════════════════════════════
   PFIE Web — style.css
   Color system:
     --primary:   #3c8dbc  (brand blue, matches Shiny app)
     --accent:    #e07a00  (brand orange, matches Shiny hover/toggle)
     --fatal:     #c0392b  (professional red)
     --nonfatal:  #2980b9  (professional blue)
     --sidebar:   #1e2d3d  (dark navy)
   ════════════════════════════════════════════════════════════════════════ */

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

/* ── CUSTOM PROPERTIES ────────────────────────────────────────────────── */
:root {
  --primary:        #3c8dbc;
  --primary-dark:   #2d6f96;
  --accent:         #e07a00;
  --accent-dark:    #c06800;
  --fatal:          #c0392b;
  --nonfatal:       #2980b9;

  --sidebar-bg:     #1e2d3d;
  --sidebar-hover:  #273f56;
  --sidebar-text:   #c8d6e4;
  --sidebar-muted:  #7a9ab5;
  --sidebar-border: rgba(255,255,255,0.08);
  --sidebar-input:  rgba(255,255,255,0.07);

  --bg:             #f0f3f7;
  --card-bg:        #ffffff;
  --border:         #dde3eb;
  --text:           #1a202c;
  --text-muted:     #5a6a7a;

  --radius:         5px;
  --shadow-sm:      0 1px 3px rgba(0,0,0,0.09);
  --shadow:         0 2px 8px rgba(0,0,0,0.11);

  --sidebar-w:      236px;
  --header-h:       50px;
  --font:           'Source Sans 3', sans-serif;
}

/* ── BASE ─────────────────────────────────────────────────────────────── */
html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--accent); text-decoration: underline; }

/* ── APP SHELL ────────────────────────────────────────────────────────── */
.app-wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;   /* fallback for browsers without svh support */
  height: 100svh;  /* mobile: never clipped by browser nav bar */
  overflow: hidden;
}

/* ── HEADER ───────────────────────────────────────────────────────────── */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 18px;
  background: var(--sidebar-bg);
  color: #fff;
  flex-shrink: 0;
  z-index: 300;
  box-shadow: 0 2px 6px rgba(0,0,0,0.28);
}

.sidebar-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 14px;
  background: var(--accent);
  color: #fff;
  border: 1.5px solid rgba(255,255,255,0.75);
  border-radius: var(--radius);
  font: 500 13px/1 var(--font);
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: var(--accent-dark); }
/* Green when sidebar is open (filters visible) */
.sidebar-toggle.sidebar-open              { background: #27ae60; }
.sidebar-toggle.sidebar-open:hover        { background: #219a52; }

.header-title {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-short { display: none; }

/* ── APP BODY ─────────────────────────────────────────────────────────── */
.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── SIDEBAR BACKDROP (mobile) ────────────────────────────────────────── */
.sidebar-backdrop {
  display: none;
  position: fixed;
  top: var(--header-h); /* don't cover the header */
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0,0,0,0.42);
  z-index: 1001; /* above Leaflet's max z-index of 1000 */
}
.sidebar-backdrop.active { display: block; }

/* ── SIDEBAR ──────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  overflow-x: hidden;
  transition: width 0.25s ease;
  z-index: 150;
}
.sidebar.collapsed { width: 0; }

.sidebar-inner {
  min-width: var(--sidebar-w);
  padding: 20px 14px 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Filter sections */
.filter-section { display: flex; flex-direction: column; gap: 6px; }

.filter-label {
  display: block;
  font: 500 11px/1 var(--font);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--sidebar-muted);
}

.filter-select {
  width: 100%;
  background: var(--sidebar-input);
  color: var(--sidebar-text);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius);
  padding: 7px 28px 7px 10px;
  font: 400 13px var(--font);
  appearance: none;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%237a9ab5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  transition: border-color 0.15s;
}
.filter-select:focus { outline: none; border-color: var(--primary); }
.filter-select option { background: #253447; }

/* Date row */
.date-row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.date-input {
  flex: 1;
  min-width: 0;
  background: var(--sidebar-input);
  color: var(--sidebar-text);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius);
  padding: 7px 8px;
  font: 400 12px var(--font);
  cursor: pointer;
  transition: border-color 0.15s;
}
.date-input:focus { outline: none; border-color: var(--primary); }
.date-sep { color: var(--sidebar-muted); font-size: 13px; flex-shrink: 0; }

/* Download button */
.download-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font: 500 13px var(--font);
  cursor: pointer;
  transition: background 0.15s;
}
.download-btn:hover { background: var(--primary-dark); }

/* Case count badge */
.case-count {
  text-align: center;
  font-size: 11px;
  color: var(--sidebar-muted);
  padding-top: 2px;
}

/* ── MAIN CONTENT ─────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── TAB NAV ──────────────────────────────────────────────────────────── */
.tab-nav {
  display: flex;
  align-items: center;
  background: var(--card-bg);
  border-bottom: 2px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 0 8px;
  gap: 0;
}
.tab-nav::-webkit-scrollbar { display: none; }

/* Sidebar toggle when living inside the tab-nav row */
.tab-nav .sidebar-toggle {
  flex-shrink: 0;
  margin-right: 4px;
  /* Inherit the existing .sidebar-toggle appearance but shrink height
     to sit comfortably within the 44px tab bar */
  padding: 5px 12px;
  font-size: 13px;
}

/* Vertical pipe divider between the filter button and the tabs */
.tab-nav-divider {
  width: 1px;
  height: 22px;
  background: var(--border);
  flex-shrink: 0;
  margin: 0 6px;
}

.tab-btn {
  padding: 0 22px;
  height: 44px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  font: 400 14px var(--font);
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--primary); }
.tab-btn.active {
  color: var(--primary);
  font-weight: 500;
  border-bottom-color: var(--accent);
}

/* ── TAB PANELS ───────────────────────────────────────────────────────── */
.tab-panel { display: none; flex: 1; overflow: hidden; min-width: 0; }
.tab-panel.active { display: flex; flex-direction: column; }

/* Scrollable inner wrapper for non-map tabs */
.tab-inner {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

/* ── MAP TOOLBAR ──────────────────────────────────────────────────────── */
.map-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 14px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.toolbar-sliders {
  display: flex;
  gap: 20px;
  flex: 1;
  min-width: 0;
}

.toolbar-slider {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 160px;
  flex: 1;
}

.slider-label {
  font: 500 12px var(--font);
  color: var(--text-muted);
  white-space: nowrap;
}

.slider-val {
  font: 600 12px var(--font);
  color: var(--primary);
  min-width: 18px;
  text-align: right;
}

.toolbar-slider input[type="range"] {
  flex: 1;
  min-width: 80px;
  accent-color: var(--primary);
  cursor: pointer;
  height: 18px;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  flex-shrink: 0;
}

/* Map buttons */
.map-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* ── MAP TAB — 3-cell grid ────────────────────────────────────────────── */
.map-grid {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: 1fr 1fr;
  grid-template-areas:
    "map trend"
    "map bar";
  min-height: 0;
}

.map-cell {
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  background: var(--bg);
}

/* Map cell: no padding so the map tiles fill to edges; overflow hidden to contain Leaflet tiles */
.map-cell--map {
  grid-area: map;
  padding: 0;
  overflow: hidden;
}
/* Plot titles rendered as HTML above each chart so they wrap naturally */
.plot-title {
  flex: none;
  padding: 2px 2px 6px;
  line-height: 1.35;
}
.plot-title-main {
  font-family: var(--font);
  font-size: 16px;
  font-weight: 700;
  color: #1a202c;
}
.plot-title-sub {
  font-family: var(--font);
  font-size: 13px;
  color: #5a6a7a;
  margin-top: 2px;
}

.trend-cell { grid-area: trend; }
.bar-cell   { grid-area: bar; }

#pfiemap {
  width: 100%;
  height: 100%;
  min-height: 220px;
  background: #e4eaf0;
}
#pfiemap.frozen { outline: 3px solid var(--fatal); }
.map-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  background: var(--primary);
  color: #fff;
  border: 1.5px solid rgba(255,255,255,0.25);
  border-radius: var(--radius);
  font: 500 12px var(--font);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
.map-btn:hover { background: var(--primary-dark); }
.map-btn.active {
  background: #7b1a1a;
  border-color: rgba(255,255,255,0.4);
}
.map-btn.active i { color: #ff9f9f; }

/* Plot areas */
.plot-area {
  flex: 1;
  min-height: 0;
  max-width: 100%;
}
/* Fix Plotly legend clip: scrollbox clip-path is sized at initial font measurement.
   When Source Sans 3 loads, legend text renders wider and gets clipped by the scrollbox.
   Removing the clip-path allows legend items to render at their true width. */
#barChart g.scrollbox,
#trendsMain g.scrollbox {
  clip-path: none !important;
}
/* ── DATA TAB ─────────────────────────────────────────────────────────── */
.table-card { overflow: hidden; }

.table-caption {
  font: 600 14px/1.4 var(--font);
  color: var(--text);
  margin-bottom: 12px;
}

.table-scroll { overflow-x: auto; }

/* DataTables overrides */
table.dataTable { border-collapse: collapse !important; font-size: 13px; }
table.dataTable thead th {
  background: #f7f9fb;
  color: var(--text);
  font-weight: 600;
  border-bottom: 2px solid var(--border) !important;
  padding: 9px 12px;
}
table.dataTable tbody td { padding: 7px 12px; vertical-align: top; }
table.dataTable tbody tr:hover { background: #f0f5fa !important; }
table.dataTable tbody td a { color: var(--primary); font-size: 12px; }
table.dataTable tbody td a:hover { color: var(--accent); text-decoration: underline; }

.dataTables_wrapper { font-family: var(--font); color: var(--text); }
.dataTables_wrapper .dt-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
}
.dataTables_wrapper .dataTables_filter { float: none; text-align: left; }
.dataTables_wrapper .dataTables_length { float: none; }
.dataTables_wrapper .dataTables_filter input,
.dataTables_wrapper .dataTables_length select {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 8px;
  font-family: var(--font);
  font-size: 13px;
}
.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_paginate { font-size: 13px; margin-top: 10px; }
.dataTables_wrapper .dataTables_paginate .paginate_button {
  padding: 3px 8px;
  border-radius: var(--radius);
  cursor: pointer;
}
.dataTables_wrapper .dataTables_paginate .paginate_button.current {
  background: var(--primary) !important;
  color: #fff !important;
  border-color: var(--primary) !important;
}
.dataTables_wrapper .dataTables_paginate .paginate_button:hover:not(.disabled):not(.current) {
  background: #f0f5fa !important;
  color: var(--primary) !important;
  border-color: var(--border) !important;
}

/* ── ABOUT TAB ────────────────────────────────────────────────────────── */
.about-card { max-width: 760px; }
.about-card h2 { font: 500 20px/1.3 var(--font); color: var(--text); margin-bottom: 14px; }
.about-card h3 { font: 500 16px/1.3 var(--font); color: var(--text); margin: 24px 0 8px; }
.about-card h4 { font: 500 14px/1.3 var(--font); color: var(--text-muted); margin: 16px 0 6px; }
.about-card p  { color: var(--text); line-height: 1.7; margin-bottom: 10px; }
.about-card ul { padding-left: 20px; margin-bottom: 10px; }
.about-card li { line-height: 1.7; margin-bottom: 2px; }

/* ── CARD ─────────────────────────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

/* ── LOADING OVERLAY ──────────────────────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 32, 46, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.3s;
}
.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.loading-box {
  text-align: center;
  color: #fff;
}
.loading-box i {
  display: block;
  font-size: 38px;
  color: var(--primary);
  margin-bottom: 16px;
}
.loading-box p { font: 400 16px var(--font); }

/* ── LEAFLET LEGEND ───────────────────────────────────────────────────── */
.leaflet-legend {
  background: rgba(255,255,255,0.94);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font: 400 13px var(--font);
  line-height: 1.5;
}
.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.legend-title {
  font-weight: 600;
  color: var(--text);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.legend-toggle {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1;
  flex-shrink: 0;
}
.legend-toggle:hover { color: var(--text); }
.legend-body {
  margin-top: 6px;
}
.leaflet-legend.collapsed .legend-body {
  display: none;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-size: 13px;
}
.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
  border: 1px solid rgba(0,0,0,0.15);
}

/* ── FLATPICKR SIDEBAR OVERRIDES ──────────────────────────────────────── */
.flatpickr-calendar {
  font-family: var(--font) !important;
  font-size: 13px !important;
  box-shadow: 0 4px 20px rgba(0,0,0,0.25) !important;
}

/* ── RESPONSIVE ───────────────────────────────────────────────────────── */

/* Desktop: ensure backdrop can never get stuck visible after a resize */
@media (min-width: 901px) {
  .sidebar-backdrop { display: none !important; }
}

/* Tablet & below: sidebar becomes a fixed overlay */
@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    height: calc(100vh - var(--header-h));
    width: var(--sidebar-w) !important; /* always full width when shown */
    transform: translateX(0);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 18px rgba(0,0,0,0.3);
    z-index: 1002; /* above Leaflet's max z-index (1000) and backdrop (1001) */
  }
  .sidebar.collapsed { transform: translateX(-100%); }

  .title-full { display: none; }
  .title-short { display: inline; }

  /* Toolbar wraps to two rows on narrow screens */
  .toolbar-sliders { flex: 1 1 100%; }
  .toolbar-divider { display: none; }
  .map-btn-row     { flex: 1 1 100%; }

  /* Single-column stack on mobile: Map → Trend → Bar.
     The whole tab panel scrolls so the toolbar scrolls away with the map. */
  #tab-map { overflow-y: auto; }
  .map-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    grid-template-areas: "map" "trend" "bar";
    flex: none;
  }
  .map-cell--map { min-height: 280px; }
  .trend-cell    { min-height: 375px; }
  .bar-cell      { min-height: 375px; }

  .sidebar-toggle.sidebar-open { position: fixed; top: 9px; right: 12px; z-index: 1003; }
  .tab-nav-divider { display: none; }
}

/* Small phones */
@media (max-width: 560px) {
  .tab-btn { padding: 0 14px; font-size: 13px; }
  .toolbar-slider { min-width: 120px; }
  .map-btn-row { flex-direction: row; flex-wrap: wrap; }
  .map-btn { flex: 0 0 calc(50% - 4px); justify-content: center; }
  .map-cell { padding: 10px 12px; }
}

/* ── LARGE SCREENS (1400px+) — scale up UI controls ~15–20% ──────────── */
@media (min-width: 1400px) {
  html, body { font-size: 16px; }

  .header-title { font-size: 19px; }

  .sidebar-toggle { font: 500 15px/1 var(--font); }
  .tab-nav .sidebar-toggle { font-size: 15px; }

  .tab-btn { font: 400 16px var(--font); }

  .filter-label { font: 500 13px/1 var(--font); }
  .filter-select { font: 400 15px var(--font); }

  .date-input { font: 400 14px var(--font); }

  .download-btn { font: 500 15px var(--font); }
}
