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

:root {
  --bg: #0a0a0a;
  --bg-panel: #111;
  --border: #1a1a1a;
  --text: #ccc;
  --text-dim: #666;
  --text-muted: #444;

  --teal: #7dd3c0;
  --lavender: #c4b5fd;
  --coral: #fca5a5;
  --sky: #93c5fd;
  --mint: #86efac;
  --indigo: #a5b4fc;

  /* cyberpunk cluster palette */
  --neon-pink: #ff2d78;
  --neon-cyan: #00f0ff;
  --neon-lime: #39ff14;
  --neon-amber: #ffb700;
  --neon-violet: #bf40ff;
  --neon-blue: #4d8bff;
  --percolating: #4ade80;
  --trending: #fbbf24;

  --font: ui-monospace, "SF Mono", Monaco, "Cascadia Code", monospace;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  font-size: 12px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* layout: grid fills space, panel on right */
.app {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* main grid view */
.grid-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  min-width: 0;
}

.grid-view canvas {
  width: min(calc(100vw - 320px - 2rem), calc(100vh - 2rem));
  height: min(calc(100vw - 320px - 2rem), calc(100vh - 2rem));
  max-width: 800px;
  max-height: 800px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none; /* custom pinch-zoom and pan */
}

/* side panel */
.panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.5rem;
  gap: 1.5rem;
  overflow-y: auto;
}

/* header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-header {
  gap: 0.75rem;
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

h1 {
  font-size: 16px;
  font-weight: normal;
  letter-spacing: 0.05em;
}

.status {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 11px;
  color: var(--text-dim);
}

.indicator {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
}

.indicator.connected {
  background: var(--percolating);
  box-shadow: 0 0 6px var(--percolating);
}

.indicator.error {
  background: var(--coral);
  box-shadow: 0 0 6px var(--coral);
}

.search-trigger {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-family: var(--font);
  font-size: 10px;
  color: var(--text-dim);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem 0.4rem;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.search-trigger:hover {
  border-color: #2a2a2a;
  color: var(--text);
}

.search-trigger:active {
  background: #0c0c0c;
}

.search-icon {
  font-size: 11px;
  line-height: 1;
}

.search-kbd {
  font-size: 9px;
  padding: 0 0.25rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-muted);
}

.search-label {
  color: var(--text-dim);
}

.search-fab {
  position: fixed;
  right: 14px;
  bottom: 14px;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(10, 10, 10, 0.9);
  color: var(--text);
  font-size: 16px;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  z-index: 60;
}

.search-fab:active {
  transform: translateY(1px);
}

.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 5, 5, 0.6);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 1rem 1rem;
  z-index: 100;
}

.search-overlay[hidden] {
  display: none;
}

.search-modal {
  width: min(560px, 94vw);
  background: rgba(10, 10, 10, 0.98);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--border);
}

#search-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 12px;
  font-family: var(--font);
}

#search-input::placeholder {
  color: var(--text-muted);
}

.search-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 0;
}

.search-results {
  max-height: 45vh;
  overflow-y: auto;
  padding: 0.4rem 0.5rem 0.6rem;
}

.search-result {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.45rem;
  border-radius: 6px;
  cursor: pointer;
}

.search-result:hover,
.search-result.active {
  background: rgba(255, 255, 255, 0.06);
}

.search-result-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.search-result-meta {
  font-size: 9px;
  color: var(--text-dim);
  flex-shrink: 0;
}

.search-empty {
  color: var(--text-muted);
  padding: 0.6rem;
  text-align: center;
  font-size: 10px;
}

.search-footer {
  padding: 0.5rem 0.8rem 0.7rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.search-hint {
  font-size: 9px;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.search-hint .search-kbd {
  font-size: 8px;
  padding: 0 0.2rem;
}

.search-sep {
  color: var(--text-muted);
  opacity: 0.6;
}

/* info section */
.info {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.description {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.6;
}

.description strong {
  color: var(--percolating);
}

.description a {
  color: var(--teal);
  text-decoration: none;
}

.description a:hover {
  text-decoration: underline;
}

/* stats row */
.stats-row {
  display: flex;
  gap: 0.75rem;
}

.stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem;
  background: var(--bg);
  border-radius: 4px;
}

.stat-value {
  font-size: 18px;
  font-weight: 500;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 9px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* entity sections - stacked vertically */
.entity-sections {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.entity-section {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.section-label {
  font-size: 9px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.rate-indicator {
  font-size: 9px;
  color: var(--text-muted);
  opacity: 0.7;
  font-variant-numeric: tabular-nums;
  cursor: default;
}

.rate-indicator.degraded {
  color: var(--coral);
  opacity: 1;
}

/* degraded alert banner */
.degraded-alert {
  background: rgba(252, 165, 165, 0.15);
  border: 1px solid var(--coral);
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 11px;
  color: var(--coral);
}

.degraded-alert[hidden] {
  display: none;
}

.degraded-alert .alert-icon {
  font-weight: bold;
  font-size: 12px;
}

.degraded-alert .alert-text {
  flex: 1;
}

/* failover alert banner */
.failover-alert {
  background: rgba(251, 191, 36, 0.12);
  border: 1px solid rgba(251, 191, 36, 0.5);
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 11px;
  color: rgb(251, 191, 36);
}

.failover-alert[hidden] {
  display: none;
}

.failover-alert .alert-icon {
  font-weight: bold;
  font-size: 12px;
}

.failover-alert .alert-text {
  flex: 1;
}

#failover-instance {
  font-weight: 600;
}

/* top entities - fixed height container */
.top-entities {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  height: 200px; /* fixed height to prevent layout jump */
  min-height: 200px;
  overflow-y: auto;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE/edge */
  background: var(--bg);
  border-radius: 4px;
  padding: 0.4rem;
}

.top-entities::-webkit-scrollbar {
  display: none; /* chrome/safari */
}

.entity-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 10px;
  padding: 0.2rem 0.3rem;
  margin: 0 -0.3rem;
  border-radius: 3px;
  transition: background 0.15s ease;
}

.entity-row:hover {
  background: rgba(255, 255, 255, 0.05);
}

/* FLIP animations for list reordering */
.entity-row-enter {
  opacity: 0;
  transform: translateX(-10px);
  transition: opacity 0.25s ease-out, transform 0.25s ease-out;
}

.entity-row-exit {
  opacity: 0;
  transform: translateX(10px);
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
  pointer-events: none;
}

.entity-rank {
  color: var(--text-muted);
  width: 1.2em;
  text-align: right;
  flex-shrink: 0;
}

.entity-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  text-decoration: none;
}

.entity-text:hover {
  text-decoration: underline;
}

.entity-count {
  color: var(--text-dim);
  font-size: 9px;
  flex-shrink: 0;
}

.entity-trend {
  font-size: 11px;
  flex-shrink: 0;
  min-width: 1.2em;
  text-align: center;
  transition: opacity 0.2s ease;
}

/* entity label colors */
.entity-label {
  font-size: 8px;
  padding: 0.1rem 0.3rem;
  border-radius: 2px;
  text-transform: uppercase;
  flex-shrink: 0;
}

.entity-label.PERSON { background: rgba(252, 165, 165, 0.2); color: var(--coral); }
.entity-label.ORG { background: rgba(147, 197, 253, 0.2); color: var(--sky); }
.entity-label.GPE { background: rgba(134, 239, 172, 0.2); color: var(--mint); }
.entity-label.PRODUCT { background: rgba(253, 230, 138, 0.2); color: var(--amber); }
.entity-label.EVENT { background: rgba(196, 181, 253, 0.2); color: var(--lavender); }
.entity-label.WORK_OF_ART { background: rgba(253, 164, 175, 0.2); color: var(--rose); }
.entity-label.FAC { background: rgba(125, 211, 192, 0.2); color: var(--teal); }
.entity-label.NORP { background: rgba(165, 180, 252, 0.2); color: #a5b4fc; }
.entity-label.LOC { background: rgba(134, 239, 172, 0.15); color: var(--mint); }

/* cluster topic pills above trending list */
.cluster-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.4rem;
}

.cluster-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 9px;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  border: 1px solid;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.cluster-pill-shape {
  font-size: 9px;
  flex-shrink: 0;
}

/* cluster shape pip on entity rows */
.cluster-pip {
  font-size: 9px;
  width: 1.2em;
  text-align: center;
  flex-shrink: 0;
  opacity: 0.85;
  cursor: default;
}

/* live feed */
.live-feed {
  height: 160px;
  overflow: hidden;
  position: relative;
  background: var(--bg);
  border-radius: 4px;
  padding: 0.4rem;
}

.live-feed::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2rem;
  background: linear-gradient(transparent, var(--bg));
  pointer-events: none;
}

.live-item {
  font-size: 10px;
  padding: 0.15rem 0;
  animation: fadeSlide 0.3s ease-out;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.live-item .entity-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

/* footer */
footer {
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

footer a {
  font-size: 10px;
  color: var(--text-dim);
  text-decoration: none;
}

footer a:hover {
  color: var(--text);
}

.footer-byline {
  font-size: 10px;
  color: var(--text-muted);
}

.footer-byline a {
  color: var(--text-dim);
}

.footer-sep {
  color: var(--text-muted);
  margin: 0 0.3rem;
}

.footer-src {
  font-size: 10px;
  margin-top: 0.3rem;
}

.footer-src a {
  color: var(--text-muted);
}

.footer-src a:hover {
  color: var(--text-dim);
}

/* haiku display — proper attributed quote */
.haiku-container {
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1.5rem 0;
  margin-bottom: 0.5rem;
  border-top: 1px solid var(--border);
}

.haiku-container.visible {
  display: flex;
}

.haiku-quote {
  text-align: center;
  margin: 0;
  padding: 0;
  border: none;
}

.haiku-text {
  font-size: 11px;
  line-height: 1.8;
  color: var(--text-dim);
  font-style: italic;
  white-space: pre-line;
}

.haiku-footer {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.4rem;
  margin-top: 0.6rem;
}

.haiku-attr {
  font-size: 9px;
  color: var(--text-muted);
  font-style: normal;
}

.haiku-time {
  font-size: 8px;
  color: transparent;
  transition: color 0.2s ease;
  font-style: normal;
}

.haiku-container:hover .haiku-time {
  color: var(--text-muted);
}

/* mobile: stack vertically */
@media (max-width: 768px) {
  .app {
    flex-direction: column;
  }

  .grid-view {
    flex: none;
    height: 50vh;
    padding: 0.5rem;
  }

  .grid-view canvas {
    width: min(calc(100vw - 1rem), calc(50vh - 1rem));
    height: min(calc(100vw - 1rem), calc(50vh - 1rem));
  }

  .panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 1rem;
    gap: 1rem;
    flex: 1;
    overflow-y: auto;
  }

  .search-trigger {
    display: none;
  }

  .search-fab {
    display: inline-flex;
    top: 14px;
    right: 14px;
    bottom: auto;
  }

  .search-overlay {
    padding: 0;
    background: var(--bg);
    backdrop-filter: none;
  }

  .search-modal {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border: none;
    border-radius: 0;
    box-shadow: none;
    display: flex;
    flex-direction: column;
  }

  .search-results {
    flex: 1;
    max-height: none;
  }

  #search-input {
    font-size: 16px; /* prevent iOS zoom */
  }

  .search-footer {
    display: none;
  }

  .description {
    font-size: 10px;
  }

  .top-entities,
  .live-feed {
    height: 120px;
    min-height: 120px;
  }

  .cluster-pills {
    gap: 0.2rem;
    margin-bottom: 0.3rem;
  }

  .cluster-pill {
    font-size: 8px;
    padding: 0.1rem 0.35rem;
  }
}

/* very small screens */
@media (max-width: 400px) {
  .stats-row {
    flex-direction: column;
    gap: 0.5rem;
  }
}

/* entity tooltip (lazy hover) */
.entity-tooltip {
  position: fixed;
  z-index: 1000;
  background: rgba(10, 10, 10, 0.95);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  font-family: var(--font);
  font-size: 10px;
  color: var(--text);
  pointer-events: none;
  max-width: 200px;
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.tooltip-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.tooltip-label {
  font-size: 8px;
  padding: 0.1rem 0.3rem;
  border-radius: 2px;
  text-transform: uppercase;
}

.tooltip-label.PERSON { background: rgba(252, 165, 165, 0.2); color: var(--coral); }
.tooltip-label.ORG { background: rgba(147, 197, 253, 0.2); color: var(--sky); }
.tooltip-label.GPE { background: rgba(134, 239, 172, 0.2); color: var(--mint); }
.tooltip-label.PRODUCT { background: rgba(253, 230, 138, 0.2); color: #fde68a; }
.tooltip-label.EVENT { background: rgba(196, 181, 253, 0.2); color: var(--lavender); }
.tooltip-label.WORK_OF_ART { background: rgba(253, 164, 175, 0.2); color: #fda4af; }
.tooltip-label.FAC { background: rgba(125, 211, 192, 0.2); color: var(--teal); }
.tooltip-label.NORP { background: rgba(165, 180, 252, 0.2); color: #a5b4fc; }
.tooltip-label.LOC { background: rgba(134, 239, 172, 0.15); color: var(--mint); }

.tooltip-name {
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tooltip-stats {
  display: flex;
  gap: 0.8rem;
  padding: 0.4rem 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.tooltip-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tooltip-value {
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
}

.tooltip-value.rising { color: var(--percolating); }
.tooltip-value.up { color: var(--text-dim); }
.tooltip-value.down { color: var(--text-muted); }
.tooltip-value.stable { color: var(--text-muted); }

.tooltip-key {
  font-size: 8px;
  color: var(--text-muted);
  text-transform: uppercase;
}

.tooltip-cluster {
  margin-top: 0.4rem;
  font-size: 9px;
  color: var(--text-dim);
}
