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

body {
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
  background: #f4f5f7; color: #333; overflow: hidden; height: 100vh; width: 100vw;
}

/* ===== Header ===== */
#header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000;
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 20px; background: #fff; border-bottom: 1px solid #e0e3e8;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.header-left { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 24px; }
#header h1 { font-size: 16px; font-weight: 700; color: #1a1a2e; }
.subtitle { font-size: 10px; color: #999; margin-top: 1px; }
.header-right { display: flex; align-items: center; gap: 14px; }
#clock { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; color: #888; }

.status { display: flex; align-items: center; gap: 6px; padding: 4px 12px; border-radius: 16px; font-size: 11px; font-weight: 600; }
.status.listening { background: #e8f9ee; border: 1px solid #b8e6c8; color: #2d9d5e; }
.status.alert-active { background: #fde8e8; border: 1px solid #f5b0b0; color: #d32f2f; animation: statusPulse 1s ease-in-out infinite; }
@keyframes statusPulse { 0%,100%{opacity:1} 50%{opacity:0.6} }
.pulse { width: 6px; height: 6px; border-radius: 50%; background: currentColor; animation: pulseDot 2s ease-in-out infinite; }
@keyframes pulseDot { 0%,100%{transform:scale(1);opacity:1} 50%{transform:scale(1.5);opacity:0.4} }

/* ===== Map ===== */
#map { position: fixed; top: 50px; left: 0; right: 300px; bottom: 0; z-index: 1; }

/* ===== Sidebar ===== */
#alert-panel {
  position: fixed; top: 50px; right: 0; width: 300px; bottom: 0;
  background: #fff; border-left: 1px solid #e0e3e8; z-index: 500;
  display: flex; flex-direction: column; overflow: hidden;
}

/* ===== Tab Bar ===== */
.tab-bar { display: flex; border-bottom: 1px solid #e8eaed; background: #fafbfc; }
.tab {
  flex: 1; padding: 8px 4px; background: none; border: none; color: #999;
  font-size: 10px; font-weight: 600; cursor: pointer; transition: all 0.2s;
  border-bottom: 2px solid transparent; font-family: inherit;
}
.tab:hover { color: #666; background: #f0f1f3; }
.tab.active { color: #d32f2f; border-bottom-color: #d32f2f; background: #fff; }
.tab-content { display: none; flex-direction: column; flex: 1; overflow: hidden; }
.tab-content.active { display: flex; }

.panel-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 14px; border-bottom: 1px solid #f0f1f3;
}
.panel-header h2 { font-size: 12px; font-weight: 700; color: #444; }
.btn-refresh {
  background: none; border: 1px solid #ddd; color: #888; font-size: 14px;
  width: 26px; height: 26px; border-radius: 6px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; transition: all 0.2s;
}
.btn-refresh:hover { background: #f0f1f3; color: #333; }
.badge {
  background: #d32f2f; color: #fff; font-size: 10px; font-weight: 700;
  min-width: 20px; height: 20px; display: flex; align-items: center; justify-content: center;
  border-radius: 10px; padding: 0 6px;
}
.badge.zero { background: #ccc; }

/* ===== LIVE TAB: Time Filter ===== */
#time-filter { padding: 10px 14px 8px; border-bottom: 1px solid #f0f1f3; background: #fafbfc; }
.filter-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.filter-row label { font-size: 10px; color: #888; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; }
.filter-value { font-size: 12px; font-weight: 700; color: #d32f2f; font-variant-numeric: tabular-nums; }
#time-slider {
  -webkit-appearance: none; appearance: none; width: 100%; height: 4px; border-radius: 2px;
  background: linear-gradient(90deg, #d32f2f 0%, #ef9a9a 50%, #e0e0e0 100%); outline: none; cursor: pointer;
}
#time-slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: #d32f2f; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.2); cursor: pointer;
}
#time-slider::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%; background: #d32f2f;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.2); cursor: pointer;
}
.filter-ticks { display: flex; justify-content: space-between; margin-top: 3px; font-size: 9px; color: #bbb; }
.filter-stats { text-align: center; font-size: 10px; color: #999; margin-top: 6px; font-variant-numeric: tabular-nums; }

/* ===== LIVE TAB: Alert List ===== */
#alert-list { flex: 1; overflow-y: auto; padding: 6px; }
#alert-list::-webkit-scrollbar { width: 3px; }
#alert-list::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }

.alert-card {
  background: #fff; border: 1px solid #f0f1f3; border-radius: 8px;
  padding: 7px 10px; margin-bottom: 4px; cursor: pointer;
  transition: all 0.15s; border-right: 3px solid #d32f2f;
}
.alert-card:hover { background: #fef5f5; box-shadow: 0 1px 4px rgba(0,0,0,0.06); }
.alert-card.fading { border-right-color: #4caf50; }
.alert-area { font-size: 11px; font-weight: 700; color: #d32f2f; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.alert-meta { display: flex; justify-content: space-between; align-items: center; font-size: 9px; color: #aaa; margin-top: 2px; }
.alert-cat { font-size: 9px; padding: 1px 6px; border-radius: 4px; background: #fef0f0; color: #d32f2f; font-weight: 500; }
.alert-time { font-variant-numeric: tabular-nums; }

/* ===== HISTORY TAB: Hour Filter ===== */
#hour-filter { padding: 8px 12px 6px; border-bottom: 1px solid #f0f1f3; background: #fafbfc; }
.hour-actions { display: flex; gap: 6px; margin-bottom: 6px; }
.hour-btn {
  flex: 1; padding: 3px 0; background: #fff; border: 1px solid #ddd; border-radius: 5px;
  font-size: 10px; font-weight: 600; color: #777; cursor: pointer; font-family: inherit; transition: all 0.15s;
}
.hour-btn:hover { background: #f0f1f3; color: #333; }
#hour-checkboxes { display: flex; flex-direction: column; gap: 1px; max-height: 260px; overflow-y: auto; }
#hour-checkboxes::-webkit-scrollbar { width: 3px; }
#hour-checkboxes::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }
.hour-check { display: flex; align-items: center; gap: 6px; padding: 3px 6px; border-radius: 5px; cursor: pointer; transition: background 0.12s; user-select: none; }
.hour-check:hover { background: #f0f1f3; }
.hour-check input[type="checkbox"] { width: 14px; height: 14px; accent-color: #d32f2f; cursor: pointer; margin: 0; flex-shrink: 0; }
.hour-label { flex: 1; font-size: 10.5px; font-weight: 500; color: #555; }
.hour-count { font-size: 10px; font-weight: 700; color: #999; min-width: 28px; text-align: left; font-variant-numeric: tabular-nums; }

/* ===== HISTORY TAB: Region Summary ===== */
#region-summary { flex: 1; overflow-y: auto; padding: 6px 10px; }
#region-summary::-webkit-scrollbar { width: 3px; }
#region-summary::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }

.summary-header { font-size: 11px; font-weight: 700; color: #555; padding: 4px 0 6px; border-bottom: 1px solid #f0f1f3; margin-bottom: 6px; }
.summary-list { display: flex; flex-direction: column; gap: 2px; }
.summary-row { display: flex; align-items: center; gap: 5px; padding: 3px 4px; border-radius: 4px; cursor: pointer; transition: background 0.12s; }
.summary-row:hover { background: #f8f8fa; }
.summary-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.summary-area { flex: 1; font-size: 10px; font-weight: 600; color: #555; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.summary-bar-bg { width: 45px; height: 3px; background: #f0f1f3; border-radius: 2px; overflow: hidden; flex-shrink: 0; }
.summary-bar-fill { height: 100%; border-radius: 2px; }
.summary-count { font-size: 10px; font-weight: 700; min-width: 24px; text-align: left; font-variant-numeric: tabular-nums; }

/* ===== Region Labels (no circle) ===== */
.region-label { background: none !important; border: none !important; }
.region-label-text {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  font-size: 13px; font-weight: 800; color: #7a6400;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 1px 1px 0 #fff, -1px -1px 0 #fff;
  white-space: nowrap; pointer-events: none;
}

/* ===== REPORTS TAB ===== */
#reports-content { flex: 1; overflow-y: auto; padding: 8px 12px; }
#reports-content::-webkit-scrollbar { width: 3px; }
#reports-content::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }

.report-section { margin-bottom: 14px; }
.section-title {
  font-size: 11px; font-weight: 700; color: #555; padding: 4px 0 6px;
  border-bottom: 1px solid #f0f1f3; margin-bottom: 6px;
}
.db-stats { font-size: 9px; color: #bbb; margin-bottom: 6px; font-variant-numeric: tabular-nums; }

/* KPIs */
#report-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 14px; }
.kpi {
  background: #fafbfc; border: 1px solid #eee; border-radius: 8px;
  padding: 8px 4px; text-align: center;
}
.kpi-val { font-size: 18px; font-weight: 800; color: #d32f2f; font-variant-numeric: tabular-nums; }
.kpi-label { font-size: 9px; color: #999; margin-top: 2px; }

/* Line chart */
.hourly-chart { width: 100%; }
.line-chart-svg { width: 100%; height: auto; font-family: inherit; }

/* Area/Hour matrix */
.matrix-wrap { overflow-x: auto; }
.matrix-table { width: 100%; border-collapse: collapse; font-size: 9px; }
.matrix-table th {
  padding: 3px 2px; font-weight: 600; color: #999; text-align: center;
  border-bottom: 1px solid #eee; font-size: 8px; white-space: nowrap;
}
.matrix-region {
  font-weight: 600; white-space: nowrap; text-align: right; padding: 2px 4px;
  max-width: 80px; overflow: hidden; text-overflow: ellipsis; font-size: 8px;
}
.matrix-place {
  font-weight: 500; white-space: nowrap; text-align: right; padding: 2px 4px;
  max-width: 100px; overflow: hidden; text-overflow: ellipsis; font-size: 8px;
  color: #555; cursor: pointer;
}
.matrix-place:hover { color: #d32f2f; text-decoration: underline; }
.matrix-cell { text-align: center; padding: 2px; font-variant-numeric: tabular-nums; color: #555; }
.matrix-total { text-align: center; padding: 2px 4px; font-weight: 700; color: #d32f2f; border-left: 1px solid #eee; }

/* Prediction cards in reports */
.predict-card {
  display: flex; gap: 8px; padding: 6px 8px; margin-bottom: 3px;
  background: #fff; border: 1px solid #f0f1f3; border-right: 3px solid #ff8800;
  border-radius: 8px; cursor: pointer; transition: all 0.15s;
}
.predict-card:hover { background: #fff8f0; box-shadow: 0 1px 4px rgba(0,0,0,0.06); }
.predict-rank { font-size: 11px; font-weight: 800; color: #ff8800; min-width: 24px; text-align: center; }
.predict-body { flex: 1; min-width: 0; }
.predict-area { font-size: 10px; font-weight: 700; color: #e67700; margin-bottom: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.predict-bar-bg { height: 3px; background: #f0f1f3; border-radius: 2px; overflow: hidden; margin-bottom: 3px; }
.predict-bar-fill { height: 100%; border-radius: 2px; transition: width 0.4s; }
.predict-meta { display: flex; gap: 6px; font-size: 9px; color: #bbb; flex-wrap: wrap; }

/* ===== Common ===== */
.empty-state { text-align: center; padding: 40px 16px; color: #bbb; }
.empty-state .icon { font-size: 36px; margin-bottom: 8px; }
.empty-state p { font-size: 11px; line-height: 1.6; }

/* ===== History Toast ===== */
.history-toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 2000;
  background: #fff; border: 1px solid #e0e3e8; color: #d32f2f;
  padding: 8px 20px; border-radius: 8px; font-size: 11px; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,0.1); white-space: nowrap;
}

/* ===== Legend ===== */
#legend {
  position: fixed; bottom: 16px; left: 16px; z-index: 500;
  background: #fff; border: 1px solid #e0e3e8; border-radius: 8px;
  padding: 8px 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 10px; color: #666; margin-bottom: 3px; }
.legend-item:last-child { margin-bottom: 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.legend-divider { height: 1px; background: #e0e3e8; margin: 6px 0; }
.legend-toggle { display: flex; align-items: center; gap: 6px; font-size: 10px; color: #555; cursor: pointer; user-select: none; font-weight: 600; }
.legend-toggle input[type="checkbox"] { width: 14px; height: 14px; accent-color: #1565c0; cursor: pointer; margin: 0; }
.nofly-details { margin-top: 5px; padding-top: 4px; border-top: 1px dashed #e0e3e8; }
.nofly-details.hidden { display: none; }

/* ===== Alert Banner ===== */
#alert-banner {
  position: fixed; top: 50px; left: 0; right: 0; z-index: 2000;
  padding: 10px 20px; background: linear-gradient(90deg, #c62828, #e53935, #c62828);
  background-size: 200% 100%; animation: bannerSlide 2s linear infinite;
  color: #fff; text-align: center; font-size: 14px; font-weight: 700;
  box-shadow: 0 2px 12px rgba(198,40,40,0.3);
}
#alert-banner.hidden { display: none; }
@keyframes bannerSlide { 0%{background-position:0% 50%} 100%{background-position:200% 50%} }
.banner-content { display: flex; align-items: center; justify-content: center; gap: 8px; }
.banner-icon { font-size: 18px; }

/* ===== Leaflet Markers ===== */
.alert-marker { background: none !important; border: none !important; position: relative; display: flex; align-items: center; justify-content: center; }
.alert-marker-inner {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 14px; height: 14px; border-radius: 50%; background: #d32f2f;
  border: 2px solid #fff; box-shadow: 0 1px 6px rgba(211,47,47,0.5);
  animation: markerPulse 1.2s ease-in-out infinite;
}
.alert-marker-ring {
  position: absolute; top: 50%; left: 50%; border-radius: 50%;
  border: 2px solid rgba(211,47,47,0.3); pointer-events: none;
  animation: ringExpand 1.5s ease-out infinite;
  width: 10px; height: 10px; margin-top: -5px; margin-left: -5px;
}
@keyframes markerPulse { 0%,100%{transform:translate(-50%,-50%) scale(1)} 50%{transform:translate(-50%,-50%) scale(1.3)} }
@keyframes ringExpand { 0%{transform:scale(1);opacity:1} 100%{transform:scale(5);opacity:0} }
.alert-marker-fading .alert-marker-inner { animation: none; transform: translate(-50%, -50%); }
.alert-marker-fading .alert-marker-ring { display: none; }

/* ===== Leaflet Controls ===== */
.leaflet-tile-pane { filter: none; }
.leaflet-control-zoom a { background: #fff !important; color: #555 !important; border-color: #ddd !important; box-shadow: 0 1px 3px rgba(0,0,0,0.08) !important; }
.leaflet-control-attribution { background: rgba(255,255,255,0.85) !important; color: #aaa !important; font-size: 9px !important; }
.leaflet-popup-content-wrapper { background: #fff !important; color: #333 !important; border-radius: 8px !important; box-shadow: 0 4px 16px rgba(0,0,0,0.12) !important; }
.leaflet-popup-tip { background: #fff !important; }
.popup-title { font-size: 12px; font-weight: 700; color: #d32f2f; margin-bottom: 2px; }
.popup-detail { font-size: 10px; color: #888; }

/* ===== Reports Mode (full width, no map) ===== */
body.reports-mode #map { display: none; }
body.reports-mode #legend { display: none; }
body.reports-mode #alert-panel { left: 0; right: 0; width: 100%; border-left: none; }
body.reports-mode #reports-content {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  padding: 14px 20px;
}
body.reports-mode .report-section { margin-bottom: 0; }
body.reports-mode .report-section:nth-child(n+4) {
  grid-column: 1 / -1;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  #map { right: 0; bottom: 200px; }
  #alert-panel { top: auto; bottom: 0; left: 0; width: 100%; height: 200px; border-left: none; border-top: 1px solid #e0e3e8; }
  body.reports-mode #reports-content { grid-template-columns: 1fr; }
}
