/* ===== 1. CSS Variables (light theme) ===== */
:root {
  --bg: #f2efe5;
  --card: #faf8f1;
  --card-hover: #f5f1e8;
  --border: #ddd8cc;
  --border-light: #e8e3d8;
  --text: #282828;
  --text-muted: #6b6b6b;
  --text-dim: #999;
  --accent: #2a6fc4;
  --accent-light: #e8f1fb;
  --error: #d93b3b;
  --error-light: #fdf0f0;
  --ok: #2d8a4e;
  --ok-light: #edf7f0;
  --warn: #c47a1a;
  --warn-light: #fef8f0;
  --cancel: #8a8a8a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.06);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.10);
  --sidebar-w: 300px;
  --topbar-h: 60px;
}

/* ===== 2. Reset & Base ===== */
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0; }
button { font: inherit; cursor: pointer; }
a { color: var(--accent); }

/* ===== 3. Top Navigation ===== */
.topnav {
  display: flex; align-items: center; gap: 20px;
  height: var(--topbar-h); padding: 0 20px;
  background: var(--card); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
}
.topnav-brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 24px; color: var(--accent); }
.brand-text { font-size: 17px; font-weight: 700; letter-spacing: 0.3px; }
.brand-text small { font-weight: 400; color: var(--text-muted); font-size: 13px; }
.ver-badge {
  display: inline-block; padding: 1px 7px; border-radius: 10px;
  font-size: 10px; font-weight: 700; vertical-align: middle; margin-left: 3px;
  background: var(--bg); color: var(--accent); text-decoration: none;
  border: 1px solid var(--border);
}
.ver-badge:hover { background: var(--accent); color: #fff; }
.ver-current { background: var(--accent); color: #fff; }

.topnav-views { display: flex; gap: 4px; background: var(--bg); border-radius: var(--radius-sm); padding: 3px; }
.view-tab {
  border: 0; background: transparent; color: var(--text-muted);
  padding: 7px 18px; border-radius: 6px; font-size: 13px; font-weight: 500;
  transition: .15s;
}
.view-tab.active { background: var(--accent); color: #fff; }
.view-tab:disabled { opacity: 0.4; cursor: not-allowed; }

.topnav-controls { margin-left: auto; display: flex; align-items: center; gap: 16px; }
.daterange { color: var(--text-muted); font-size: 12px; }
.link-btn { background: none; border: 0; color: var(--accent); font-size: 12px; padding: 4px 8px; border-radius: 4px; }
.link-btn:hover { background: var(--accent-light); }

.langtoggle { display: flex; background: var(--bg); border-radius: var(--radius-sm); padding: 3px; }
.lbtn {
  border: 0; background: transparent; color: var(--text-muted);
  padding: 5px 10px; border-radius: 5px; font-weight: 600; font-size: 12px;
  transition: .15s;
}
.lbtn.active { background: var(--accent); color: #fff; }

/* ===== 4. Layout ===== */
.layout { display: flex; height: calc(100vh - var(--topbar-h)); }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--card); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.content { flex: 1; overflow-y: auto; padding: 20px; }

/* ===== 5. Sidebar ===== */
.sidebar-tabs { display: flex; gap: 2px; background: var(--bg); border-radius: var(--radius-sm); padding: 3px; }
.stab {
  flex: 1; border: 0; background: transparent; color: var(--text-muted);
  padding: 7px 10px; border-radius: 5px; font-size: 12px; font-weight: 600;
  transition: .15s;
}
.stab.active { background: var(--accent); color: #fff; }

.search-wrap input {
  width: 100%; padding: 9px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font-size: 13px;
}
.search-wrap input::placeholder { color: var(--text-dim); }
.search-wrap input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(42,111,196,0.1); }

.date-range { display: flex; gap: 6px; align-items: center; }
.date-range input {
  flex: 1; padding: 6px 8px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font-size: 12px;
}
.date-range span { color: var(--text-dim); font-size: 12px; }

.filter-actions { display: flex; justify-content: space-between; align-items: center; }
.result-count { color: var(--text-muted); font-size: 12px; }

.facets { flex: 1; overflow-y: auto; }
.facet { border-top: 1px solid var(--border-light); padding: 8px 0; }
.facet-head {
  display: flex; justify-content: space-between; align-items: center;
  width: 100%; background: none; border: 0; color: var(--text);
  font-weight: 600; font-size: 13px; padding: 5px 2px; text-align: left;
}
.facet-head .chev { color: var(--text-dim); transition: .15s; font-size: 11px; }
.facet.collapsed .chev { transform: rotate(-90deg); }
.facet.collapsed .facet-body { display: none; }
.facet-body { padding: 4px 2px 2px; max-height: 210px; overflow-y: auto; }
.facet-opt {
  display: flex; align-items: center; gap: 8px; padding: 4px 2px;
  cursor: pointer; border-radius: 4px;
}
.facet-opt:hover { background: var(--bg); }
.facet-opt input { accent-color: var(--accent); }
.facet-opt .lbl { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.facet-opt .cnt { color: var(--text-dim); font-size: 11px; }
.facet-opt.sel-error .lbl { color: var(--error); font-weight: 600; }

/* sidebar stats */
.sidebar-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  border-top: 1px solid var(--border); padding-top: 12px;
}
.sidebar-stat {
  background: var(--bg); border-radius: var(--radius-sm); padding: 10px 12px; text-align: center;
}
.sidebar-stat .sv { font-size: 20px; font-weight: 700; }
.sidebar-stat .sk { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.sidebar-stat.err .sv { color: var(--error); }
.sidebar-stat.ok .sv { color: var(--ok); }

/* ===== 6. KPI Cards ===== */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px; margin-bottom: 20px;
}
.kpi {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px;
  box-shadow: var(--shadow);
}
.kpi .v { font-size: 28px; font-weight: 700; }
.kpi .k { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.kpi .sub { color: var(--text-dim); font-size: 11px; margin-top: 2px; }
.kpi.err .v { color: var(--error); }
.kpi.ok .v { color: var(--ok); }
.kpi.accent .v { color: var(--accent); }

/* ===== 7. Chart Grid & Panels ===== */
.grid {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 16px; margin-bottom: 20px;
}
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }

.panel {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow);
}
.panel h2 {
  font-size: 12px; color: var(--text-muted); font-weight: 600;
  margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.4px;
}
.map-panel { grid-row: span 2; }
#map { height: 420px; border-radius: var(--radius-sm); background: #eae5da; }
.leaflet-container { background: #eae5da; outline: none; }
.chart-box { position: relative; height: 220px; }
.chart-box.tall { height: 280px; }

.legend { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; font-size: 11px; color: var(--text-muted); }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }

.map-hint { color: var(--text-dim); font-size: 11px; margin-top: 6px; text-align: center; }

/* ===== 8. Feed View ===== */
.feed-view { max-width: 900px; margin: 0 auto; }

.filter-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.filter-chip {
  padding: 6px 14px; border-radius: 20px; border: 1px solid var(--border);
  background: var(--card); color: var(--text-muted); font-size: 12px;
  font-weight: 500; transition: .15s;
}
.filter-chip:hover { border-color: var(--accent); color: var(--accent); }
.filter-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.filter-chip .chip-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }

.date-header {
  color: var(--text-muted); font-size: 13px; font-weight: 600;
  padding: 14px 0 8px; border-bottom: 1px solid var(--border-light); margin-bottom: 12px;
  position: sticky; top: 0; background: var(--bg); z-index: 5;
}
.date-header:first-child { padding-top: 0; }

.feed-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 10px;
  cursor: pointer; box-shadow: var(--shadow); transition: .15s;
}
.feed-card:hover { border-color: var(--accent); box-shadow: var(--shadow-lg); }
.feed-card-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.feed-card-sender {
  padding: 4px 10px; border-radius: 6px; font-size: 11px; font-weight: 600;
  background: var(--accent-light); color: var(--accent);
}
.feed-card-risk { font-size: 13px; font-weight: 600; color: var(--text); }
.feed-card-county { font-size: 12px; color: var(--text-muted); }
.feed-card-msg {
  font-size: 13px; color: var(--text); margin-bottom: 8px;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.feed-card-footer { display: flex; align-items: center; gap: 10px; }
.feed-card-time { font-size: 11px; color: var(--text-dim); }

.feed-sidebar { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.feed-sidebar-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; }
.feed-sidebar-row .sent { color: var(--ok); font-weight: 600; }
.feed-sidebar-row .failed { color: var(--error); font-weight: 600; }
.feed-breakdown { margin-top: 12px; }
.feed-breakdown h4 { font-size: 11px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 6px; }
.feed-breakdown-item { display: flex; justify-content: space-between; font-size: 13px; padding: 2px 0; }

/* ===== 9. Location View ===== */
.location-view { max-width: 1100px; margin: 0 auto; }
.location-header {
  display: flex; align-items: center; gap: 16px; margin-bottom: 20px;
}
.back-btn {
  border: 0; background: var(--card); color: var(--accent);
  padding: 8px 16px; border-radius: var(--radius-sm); font-size: 13px; font-weight: 500;
  border: 1px solid var(--border);
}
.back-btn:hover { background: var(--accent-light); }
.location-county-name { font-size: 22px; font-weight: 700; }
.location-county-count { color: var(--text-muted); font-size: 16px; font-weight: 400; }

.locality-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.locality-chip {
  padding: 5px 12px; border-radius: 16px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font-size: 12px;
  cursor: pointer; transition: .15s;
}
.locality-chip:hover { border-color: var(--accent); }
.locality-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.locality-count { color: var(--text-dim); font-size: 11px; margin-left: 4px; }

.location-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 20px;
}
.location-feed { margin-top: 20px; }

/* ===== 10. Detail Drawer ===== */
.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.3); z-index: 90; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(520px, 94vw);
  background: var(--card); border-left: 1px solid var(--border); box-shadow: var(--shadow-lg);
  z-index: 100; overflow-y: auto; padding: 24px;
}
.drawer-close { position: absolute; top: 12px; right: 16px; background: none; border: 0;
  color: var(--text-muted); font-size: 26px; line-height: 1; }
.drawer-close:hover { color: var(--text); }
.drawer h3 { margin: 18px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--text-muted); }
.drawer .msg-block { background: var(--bg); border: 1px solid var(--border-light); border-radius: var(--radius-sm); padding: 14px; white-space: pre-wrap; font-size: 13px; }
.meta-grid { display: grid; grid-template-columns: 130px 1fr; gap: 5px 12px; font-size: 13px; }
.meta-grid .mk { color: var(--text-muted); }
.drawer-title { font-size: 18px; font-weight: 700; padding-right: 30px; }
.drawer-sub { color: var(--text-muted); margin: 4px 0 6px; font-size: 13px; }

.hidden { display: none !important; }

/* ===== 11. Badges ===== */
.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge.sent { background: var(--ok-light); color: var(--ok); }
.badge.error { background: var(--error-light); color: var(--error); }
.badge.cancel { background: #f0f0f0; color: var(--cancel); }
.badge.sev-extreme { background: var(--error-light); color: var(--error); }
.badge.sev-severe { background: var(--warn-light); color: var(--warn); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px; }
.pill {
  display: inline-block; background: var(--bg); border: 1px solid var(--border-light);
  border-radius: 6px; padding: 3px 8px; margin: 2px 4px 2px 0; font-size: 12px;
}

/* status badge in feed */
.status-row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.status-badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px;
  border-radius: 14px; font-size: 11px; font-weight: 600; }
.status-badge.sent { background: var(--ok-light); color: var(--ok); }
.status-badge.error { background: var(--error-light); color: var(--error); }

/* ===== 12. Empty States ===== */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.5; }
.empty-state p { font-size: 15px; margin-bottom: 12px; }

/* ===== 13. Responsive ===== */
@media (max-width: 1200px) {
  .grid, .location-grid { grid-template-columns: 1fr; }
  .map-panel { grid-row: auto; }
}
@media (max-width: 768px) {
  .sidebar { display: none; }
  .layout { flex-direction: column; }
  .topnav { flex-wrap: wrap; height: auto; padding: 10px 12px; gap: 10px; }
  .topnav-views { order: 3; width: 100%; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}

/* ===== 14. Footer ===== */
.site-footer {
  text-align: center; padding: 14px 18px; border-top: 1px solid var(--border);
  color: var(--text-muted); font-size: 12px; background: var(--card);
}
.site-footer a { color: var(--accent); text-decoration: none; }
.site-footer a:hover { text-decoration: underline; }
.footer-range { margin-left: 8px; opacity: 0.6; }
