* {
  box-sizing: border-box;
}

:root {
  --bg-cream: #f8f3e7;
  --bg-wash: #eef4ef;
  --ink-900: #1f2a1f;
  --ink-700: #425346;
  --ink-500: #5e6d62;
  --card: rgba(255, 255, 255, 0.85);
  --card-border: rgba(27, 59, 45, 0.1);
  --accent: #1a6d5a;
  --accent-strong: #145646;
  --warning: #df5f2d;
  --shadow-lg: 0 20px 40px rgba(20, 40, 26, 0.12);
  --shadow-sm: 0 8px 24px rgba(20, 40, 26, 0.08);
  --threat-green: #10b981;
  --threat-yellow: #f59e0b;
  --threat-red: #ef4444;
  --risk-glow: rgba(26, 109, 90, 0.22);
  --ticker-bg: rgba(255, 255, 255, 0.58);
  --ticker-border: rgba(26, 109, 90, 0.25);
}

[data-theme="dark"] {
  --bg-cream: #0f172a;
  --bg-wash: #1e293b;
  --ink-900: #f1f5f9;
  --ink-700: #cbd5e1;
  --ink-500: #94a3b8;
  --card: rgba(30, 41, 59, 0.85);
  --card-border: rgba(148, 163, 184, 0.15);
  --risk-glow: rgba(45, 212, 191, 0.22);
  --ticker-bg: rgba(15, 23, 42, 0.6);
  --ticker-border: rgba(148, 163, 184, 0.3);
}

[data-risk="elevated"] {
  --accent: #c26a1a;
  --accent-strong: #a05312;
  --warning: #ef7f1a;
  --risk-glow: rgba(239, 127, 26, 0.35);
}

[data-risk="critical"] {
  --accent: #b63c2b;
  --accent-strong: #8e2a1d;
  --warning: #f97316;
  --risk-glow: rgba(239, 68, 68, 0.4);
}

body {
  margin: 0;
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 12% 12%, rgba(26, 109, 90, 0.18), transparent 24%),
    radial-gradient(circle at 88% 7%, rgba(223, 95, 45, 0.15), transparent 22%),
    linear-gradient(180deg, var(--bg-cream), var(--bg-wash));
  color: var(--ink-900);
  min-height: 100vh;
}

.bg-orb {
  position: fixed;
  z-index: -1;
  width: 360px;
  height: 360px;
  border-radius: 999px;
  filter: blur(44px);
  opacity: 0.6;
  pointer-events: none;
}

.orb-1 {
  top: -120px;
  left: -70px;
  background: rgba(223, 95, 45, 0.35);
}

.orb-2 {
  bottom: -140px;
  right: -100px;
  background: rgba(26, 109, 90, 0.35);
}

header {
  margin: 20px auto 0;
  width: min(1240px, calc(100% - 32px));
  color: #f4f8f5;
  background: linear-gradient(120deg, #0f3b30, #1a6d5a 45%, #df5f2d 120%);
  border-radius: 20px;
  padding: 28px 28px;
  box-shadow: var(--shadow-lg);
  animation: lift-in 650ms ease;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  overflow: hidden;
}

[data-theme="dark"] header {
  background: linear-gradient(120deg, #0f172a, #1e293b 45%, #7c2d12 120%);
}

header > div:first-child {
  flex: 1;
}

.header-controls {
  display: flex;
  gap: 12px;
  align-items: center;
}

.theme-toggle {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #f4f8f5;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 18px;
  transition: background 200ms ease;
}

.theme-toggle:hover {
  background: rgba(255, 255, 255, 0.3);
}

header h1 {
  margin: 0 0 4px;
  font-size: clamp(1.5rem, 2.4vw, 2.25rem);
  letter-spacing: 0.4px;
}

header p {
  margin: 0;
  color: rgba(244, 248, 245, 0.88);
}

.status-ribbon {
  margin-top: 14px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.28);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
}

.status-pill {
  font-family: "IBM Plex Mono", monospace;
  font-size: 11px;
  letter-spacing: 0.8px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(16, 185, 129, 0.24);
  border: 1px solid rgba(255, 255, 255, 0.35);
}

[data-risk="elevated"] .status-pill {
  background: rgba(245, 158, 11, 0.24);
}

[data-risk="critical"] .status-pill {
  background: rgba(239, 68, 68, 0.24);
  animation: status-pulse 1.2s ease-in-out infinite;
}

.status-ribbon #riskStateText {
  font-size: 13px;
  color: rgba(248, 252, 250, 0.92);
}

main {
  width: min(1240px, calc(100% - 32px));
  margin: 18px auto 28px;
  display: grid;
  gap: 20px;
}

.webhook-alert {
  display: none;
  border-radius: 14px;
  border: 1px solid rgba(176, 16, 16, 0.35);
  background: linear-gradient(120deg, rgba(168, 21, 21, 0.94), rgba(215, 42, 42, 0.9));
  color: #fff;
  padding: 12px 14px;
  font-weight: 600;
  letter-spacing: 0.2px;
  box-shadow: 0 10px 24px rgba(141, 17, 17, 0.25);
}

.webhook-alert-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.webhook-alert.is-visible {
  display: block;
  animation: alert-pop 300ms ease;
}

.webhook-alert.is-shocking {
  animation: alert-shock 640ms ease;
}

.live-ticker-card {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
}

.live-ticker-label {
  font-family: "IBM Plex Mono", monospace;
  color: var(--ink-700);
  font-size: 12px;
  letter-spacing: 0.7px;
  text-transform: uppercase;
}

.live-ticker {
  border-radius: 12px;
  border: 1px solid var(--ticker-border);
  background: var(--ticker-bg);
  overflow: hidden;
  position: relative;
}

.live-ticker::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background:
    radial-gradient(120% 180% at 50% 50%, transparent 60%, rgba(255, 255, 255, 0.08) 100%),
    linear-gradient(90deg, rgba(255, 255, 255, 0.08), transparent 12%, transparent 88%, rgba(255, 255, 255, 0.08));
}

[data-theme="dark"] .live-ticker::after {
  background:
    radial-gradient(120% 180% at 50% 50%, transparent 58%, rgba(148, 163, 184, 0.08) 100%),
    linear-gradient(90deg, rgba(148, 163, 184, 0.06), transparent 12%, transparent 88%, rgba(148, 163, 184, 0.06));
}

.live-ticker-track {
  --ticker-duration: 36s;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 100%;
  width: max-content;
  padding: 10px;
  animation: ticker-scroll var(--ticker-duration) linear infinite;
}

.live-ticker:hover .live-ticker-track {
  animation-play-state: paused;
}

.ticker-item {
  white-space: nowrap;
  border: 1px solid rgba(26, 109, 90, 0.28);
  color: var(--ink-900);
  background: rgba(255, 255, 255, 0.74);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  font-family: "IBM Plex Mono", monospace;
}

[data-theme="dark"] .ticker-item {
  background: rgba(15, 23, 42, 0.84);
  border-color: rgba(148, 163, 184, 0.38);
  color: var(--ink-700);
}

.ticker-item:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.alert-see-map-btn {
  background: #fff;
  color: #a81515;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 8px;
}

.alert-see-map-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 12px rgba(80, 0, 0, 0.2);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.layout {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
}

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 16px;
  border: 1px solid var(--card-border);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(6px);
  animation: fade-up 450ms ease both;
}

[data-risk="elevated"] .card {
  box-shadow: var(--shadow-sm), 0 0 0 1px rgba(245, 158, 11, 0.15);
}

[data-risk="critical"] .card {
  box-shadow: var(--shadow-sm), 0 0 0 1px rgba(239, 68, 68, 0.2), 0 0 36px var(--risk-glow);
}

.card h2,
.card h3 {
  margin-top: 0;
}

.card h2 {
  margin-bottom: 10px;
}

.card h3 {
  margin-bottom: 6px;
  color: var(--ink-700);
}

.card p {
  margin: 0;
}

.stats-grid .card p {
  font-family: "IBM Plex Mono", monospace;
  font-size: clamp(1.6rem, 2.4vw, 2.1rem);
  color: var(--ink-900);
}

.live-counter {
  position: relative;
}

.live-counter::before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 12px;
  height: 12px;
  background: #ef4444;
  border-radius: 50%;
  animation: pulse-dot 2s ease-in-out infinite;
}

[data-risk="critical"] .live-counter::before {
  animation-duration: 0.8s;
}

@keyframes pulse-dot {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.muted {
  color: var(--ink-500);
  margin-bottom: 12px;
}

.map-card {
  min-height: 500px;
}

.map-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.map-window-controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.map-window-btn {
  background: rgba(26, 109, 90, 0.12);
  color: var(--accent);
  border: 1px solid rgba(26, 109, 90, 0.35);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
}

.map-window-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

[data-theme="dark"] .map-window-btn {
  background: rgba(148, 163, 184, 0.12);
  border-color: rgba(148, 163, 184, 0.35);
  color: var(--ink-700);
}

[data-theme="dark"] .map-window-btn.active {
  background: #0ea5a4;
  border-color: #0ea5a4;
  color: #052023;
}

.map-window-btn:hover {
  transform: translateY(-1px);
}

.map-window-btn:focus-visible {
  outline: 2px solid rgba(223, 95, 45, 0.5);
  outline-offset: 2px;
}

#toggleMapHeatmap {
  margin-left: auto;
}

@media (max-width: 900px) {
  #toggleMapHeatmap {
    margin-left: 0;
  }
}

#map {
  width: 100%;
  height: 430px;
  border-radius: 12px;
  margin-top: 10px;
  border: 1px solid rgba(26, 109, 90, 0.2);
}

.detection-cluster {
  width: 48px !important;
  height: 48px !important;
  margin-left: -24px;
  margin-top: -24px;
  display: block;
  pointer-events: none;
}

.detection-cluster .cluster-badge {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.82);
  box-shadow:
    0 8px 18px rgba(20, 40, 26, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  color: #fff;
  font-family: "IBM Plex Mono", monospace;
  font-weight: 700;
  position: relative;
  overflow: hidden;
}

.detection-cluster .cluster-badge::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: inherit;
  background: radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.24), transparent 36%);
  pointer-events: none;
}

.detection-cluster .cluster-count {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: 0.95rem;
  line-height: 1;
  text-shadow: 0 1px 2px rgba(15, 23, 42, 0.25);
}

.detection-cluster-low .cluster-badge {
  background: linear-gradient(180deg, #34d399 0%, #059669 100%);
}

.detection-cluster-mid .cluster-badge {
  background: linear-gradient(180deg, #facc15 0%, #eab308 100%);
  color: #1f2937;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.25);
}

.detection-cluster-high .cluster-badge {
  background: linear-gradient(180deg, #f59e0b 0%, #ea580c 100%);
}

.detection-cluster-critical .cluster-badge {
  background: linear-gradient(180deg, #ef4444 0%, #b91c1c 100%);
}

[data-theme="dark"] .detection-cluster {
  filter: saturate(0.95) brightness(0.98);
}

[data-theme="dark"] .detection-cluster-low .cluster-badge {
  background: linear-gradient(180deg, #34d399 0%, #047857 100%);
}

[data-theme="dark"] .detection-cluster-mid .cluster-badge {
  background: linear-gradient(180deg, #facc15 0%, #ca8a04 100%);
  color: #111827;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.18);
}

[data-theme="dark"] .detection-cluster-high .cluster-badge {
  background: linear-gradient(180deg, #f97316 0%, #c2410c 100%);
}

[data-theme="dark"] .detection-cluster-critical .cluster-badge {
  background: linear-gradient(180deg, #fb7185 0%, #be123c 100%);
}

.side-card {
  max-height: 500px;
  overflow: auto;
}

.side-card {
  scrollbar-width: thin;
  scrollbar-color: rgba(148, 163, 184, 0.7) rgba(30, 41, 59, 0.55);
}

.side-card::-webkit-scrollbar {
  width: 10px;
}

.side-card::-webkit-scrollbar-track {
  background: rgba(30, 41, 59, 0.45);
  border-radius: 999px;
}

.side-card::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.72);
  border-radius: 999px;
  border: 2px solid rgba(30, 41, 59, 0.45);
}

.side-card::-webkit-scrollbar-thumb:hover {
  background: rgba(203, 213, 225, 0.85);
}

[data-theme="light"] .side-card {
  scrollbar-color: rgba(148, 163, 184, 0.85) rgba(241, 245, 249, 0.9);
}

[data-theme="light"] .side-card::-webkit-scrollbar-track {
  background: rgba(241, 245, 249, 0.9);
}

[data-theme="light"] .side-card::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.9);
  border-color: rgba(241, 245, 249, 0.9);
}

.event-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}

.event-item {
  border: 1px solid rgba(26, 109, 90, 0.22);
  border-radius: 10px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  transition: transform 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
  border-left: 4px solid #cbd5e1;
}

.event-item.threat-green {
  border-left-color: var(--threat-green);
  background: rgba(16, 185, 129, 0.08);
}

.event-item.threat-yellow {
  border-left-color: var(--threat-yellow);
  background: rgba(245, 158, 11, 0.08);
}

.event-item.threat-red {
  border-left-color: var(--threat-red);
  background: rgba(239, 68, 68, 0.08);
}

[data-theme="dark"] .event-item {
  background: rgba(55, 65, 81, 0.9);
  border-color: rgba(148, 163, 184, 0.22);
}

.event-item strong {
  display: block;
  margin-bottom: 6px;
}

.event-item:hover {
  transform: translateY(-2px);
  border-color: rgba(26, 109, 90, 0.5);
}

.event-item.is-selected {
  border-color: rgba(223, 95, 45, 0.9);
  box-shadow: 0 0 0 2px rgba(223, 95, 45, 0.18);
  background: rgba(255, 244, 238, 0.95);
}

.event-item:focus-visible {
  outline: 2px solid rgba(223, 95, 45, 0.5);
  outline-offset: 2px;
}

ul {
  padding-left: 18px;
}

li {
  margin-bottom: 6px;
  color: var(--ink-700);
}

.chart-card {
  padding: 18px;
}

.chart-topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.timeline-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: "IBM Plex Mono", monospace;
  color: var(--ink-700);
}

.timeline-controls select {
  border: 1px solid rgba(26, 109, 90, 0.24);
  border-radius: 10px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.9);
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  color: var(--ink-900);
}

[data-theme="dark"] .timeline-controls select {
  border: 1px solid rgba(148, 163, 184, 0.3);
  background: rgba(15, 23, 42, 0.82);
}

.chart-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 14px;
}

.chart-panel {
  border: 1px solid rgba(26, 109, 90, 0.14);
  border-radius: 14px;
  padding: 14px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(249, 252, 250, 0.86));
}

[data-theme="dark"] .chart-panel {
  border: 1px solid rgba(148, 163, 184, 0.2);
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.86), rgba(30, 41, 59, 0.8));
}

.chart-panel-wide {
  grid-column: 1 / -1;
}

.chart-panel canvas {
  width: 100%;
  height: 290px !important;
}

.chart-toggle-controls {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.chart-toggle-btn {
  background: rgba(26, 109, 90, 0.2);
  color: var(--accent);
  border: 1px solid var(--accent);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  transition: all 200ms ease;
}

.chart-toggle-btn.active {
  background: var(--accent);
  color: #fff;
}

.chart-toggle-btn:hover {
  background: var(--accent);
  color: #fff;
}

.hourly-heatmap-container {
  display: none;
}

.hourly-heatmap-container.visible {
  display: block;
}

.export-controls {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.export-btn {
  background: linear-gradient(120deg, #8b5cf6, #6366f1);
  padding: 8px 12px;
  font-size: 12px;
}

button {
  border: none;
  background: linear-gradient(120deg, var(--accent), var(--accent-strong));
  color: white;
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  letter-spacing: 0.2px;
  transition: transform 180ms ease, box-shadow 180ms ease;
}

button:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 14px rgba(26, 109, 90, 0.22);
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes lift-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes alert-pop {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes status-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4);
  }
  70% {
    box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);
  }
}

@keyframes ticker-scroll {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-50%);
  }
}

@keyframes alert-shock {
  0% {
    transform: translateY(-6px) scale(0.98);
    filter: saturate(1.2);
  }
  40% {
    transform: translateY(0) scale(1.01);
  }
  100% {
    transform: translateY(0) scale(1);
    filter: saturate(1);
  }
}

@media (max-width: 900px) {
  header,
  main {
    width: calc(100% - 20px);
  }

  header {
    margin-top: 10px;
    border-radius: 14px;
    padding: 20px;
  }

  .chart-grid,
  .layout {
    grid-template-columns: 1fr;
  }

  #map {
    height: 350px;
  }

  .chart-panel canvas {
    height: 250px !important;
  }
}