:root {
  --font-body: "Segoe UI Variable", "Segoe UI", "Tahoma", sans-serif;
  --font-display: "Bahnschrift", "Segoe UI", sans-serif;
  --bg-0: #0a0f14;
  --bg-1: #101826;
  --bg-2: #0d1520;
  --panel: rgba(14, 20, 30, 0.92);
  --panel-border: rgba(125, 170, 220, 0.16);
  --accent: #7dd3ff;
  --accent-2: #41e2d1;
  --accent-3: #ffb454;
  --text: #e7eef7;
  --text-dim: #9bb0c5;
  --danger: #ff6b5c;
  --shadow: 0 28px 60px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--font-body);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background:
    radial-gradient(circle at 12% 20%, rgba(125, 211, 255, 0.2), transparent 42%),
    radial-gradient(circle at 82% 18%, rgba(65, 226, 209, 0.18), transparent 46%),
    radial-gradient(circle at 30% 78%, rgba(255, 180, 84, 0.16), transparent 52%),
    linear-gradient(160deg, var(--bg-1), var(--bg-2));
  z-index: -2;
}

body::after {
  content: "";
  position: fixed;
  inset: 0;
  background-image: radial-gradient(rgba(180, 210, 240, 0.12) 1px, transparent 0);
  background-size: 160px 160px;
  opacity: 0.14;
  z-index: -1;
}

#root {
  min-height: 100vh;
}

.boot-status {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-size: 0.85rem;
  color: var(--text-dim);
  background: radial-gradient(circle at 30% 30%, rgba(125, 211, 255, 0.18), transparent 55%);
  z-index: 2;
}

.boot-status.error {
  color: var(--danger);
}

.app {
  min-height: 100vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 12px 24px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(10, 15, 21, 0.92);
  backdrop-filter: blur(18px);
}

.top-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  width: 100%;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.brand-title {
  font-family: var(--font-display);
  letter-spacing: 0.22em;
  text-transform: uppercase;
  font-size: 0.95rem;
}

.brand-sub {
  color: var(--text-dim);
  font-size: 0.8rem;
}

.query-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.query-input {
  width: 220px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(125, 211, 255, 0.2);
  background: rgba(14, 20, 30, 0.9);
  color: var(--text);
}

.query-topn {
  width: 64px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(125, 211, 255, 0.2);
  background: rgba(14, 20, 30, 0.9);
  color: var(--text);
}

.btn {
  border-radius: 10px;
  border: 1px solid rgba(125, 170, 220, 0.2);
  padding: 8px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  background: rgba(18, 26, 40, 0.9);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.2s ease, filter 0.2s ease, border-color 0.2s ease;
}

.btn:hover {
  filter: brightness(1.03);
  border-color: rgba(125, 170, 220, 0.4);
}

.btn.primary {
  background: linear-gradient(135deg, #3a91ff, var(--accent));
  color: #081018;
  border-color: transparent;
}

.btn.ghost {
  background: rgba(12, 18, 28, 0.7);
  color: var(--text-dim);
}

.btn.slim {
  padding: 6px 10px;
  font-size: 0.72rem;
}

.main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  grid-template-rows: minmax(0, 1fr);
  gap: 16px;
  padding: 16px 24px 24px;
  min-height: 0;
}

.graph-panel {
  position: relative;
  min-height: 60vh;
  height: 100%;
  border-radius: 24px;
  overflow: hidden;
  border: 1px solid var(--panel-border);
  background: radial-gradient(circle at 20% 20%, rgba(30, 50, 80, 0.4), transparent 60%),
    linear-gradient(160deg, rgba(7, 12, 18, 0.96), rgba(14, 20, 30, 0.95));
  box-shadow: var(--shadow);
}

@media (max-width: 820px) {
  .top-bar {
    padding: 10px 16px;
  }

  .top-controls {
    gap: 8px;
  }

  .file-input.file-top {
    flex: 1 1 220px;
    min-width: 0;
  }

  .btn {
    padding: 7px 10px;
  }

  .main {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 12px 16px 18px;
  }

  .graph-panel {
    min-height: 54vh;
    height: 54vh;
  }

  .side-panel {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .panel-section {
    margin: 0;
  }

  .legend-row {
    font-size: 0.8rem;
  }

  .meta-box {
    max-height: 120px;
  }

  .sub-hud {
    top: 56px;
    width: 220px;
  }
}

@media (max-width: 520px) {
  .top-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .file-input.file-top,
  .btn {
    width: 100%;
  }

  .graph-panel {
    min-height: 52vh;
    height: 52vh;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }

  .sub-hud {
    position: static;
    width: 100%;
    margin-top: 12px;
  }
}

.graph-canvas {
  position: absolute;
  inset: 0;
}

/* Ensure R3F canvas always has a drawable size. */
.graph-canvas canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

.graph-status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.hud {
  position: absolute;
  left: 16px;
  top: 16px;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid rgba(125, 170, 220, 0.2);
  background: rgba(12, 18, 28, 0.85);
  backdrop-filter: blur(14px);
  font-size: 0.75rem;
  min-width: 180px;
}

.hud.slim {
  padding: 6px 12px;
  border-radius: 12px;
  min-width: 0;
  max-width: calc(100% - 32px);
}

.hud-line {
  color: var(--text-dim);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
}

.hud-title {
  font-family: var(--font-display);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent-2);
  font-size: 0.62rem;
}

.hud-row {
  margin-top: 6px;
  color: var(--text-dim);
}

.sub-hud {
  position: absolute;
  right: 16px;
  top: 64px;
  width: 260px;
  display: grid;
  grid-auto-rows: min-content;
  gap: 10px;
  padding: 12px;
  border-radius: 16px;
  border: 1px solid rgba(125, 170, 220, 0.2);
  background: rgba(10, 16, 24, 0.86);
  backdrop-filter: blur(12px);
  pointer-events: auto;
  max-height: calc(100% - 32px);
}

.sub-hud-col {
  border-radius: 12px;
  border: 1px solid rgba(125, 170, 220, 0.14);
  background: rgba(9, 14, 22, 0.82);
  padding: 10px 12px;
  min-height: 88px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 6px;
}

.sub-hud-col.takeaways {
  border-color: rgba(127, 226, 177, 0.35);
  box-shadow: 0 10px 22px rgba(78, 182, 138, 0.18);
}

.sub-hud-col.remember {
  border-color: rgba(255, 209, 102, 0.35);
  box-shadow: 0 10px 22px rgba(218, 176, 78, 0.18);
}

.sub-hud-col.keywords {
  border-color: rgba(183, 243, 107, 0.32);
  box-shadow: 0 10px 22px rgba(125, 190, 74, 0.18);
}

.sub-hud-title {
  font-family: var(--font-display);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.62rem;
  margin: 0;
}

.sub-hud-desc {
  font-size: 0.67rem;
  color: rgba(200, 215, 235, 0.68);
  margin: 0;
}

.sub-hud-col.takeaways .sub-hud-title {
  color: #7fe2b1;
}

.sub-hud-col.remember .sub-hud-title {
  color: #ffd166;
}

.sub-hud-col.keywords .sub-hud-title {
  color: #b7f36b;
}

.sub-hud-body {
  font-size: 0.72rem;
  color: var(--text-dim);
  white-space: pre-line;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.side-panel {
  border-radius: 22px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-shadow: var(--shadow);
  max-height: 100%;
  overflow: auto;
}

.panel-section {
  border-radius: 16px;
  border: 1px solid rgba(125, 170, 220, 0.16);
  background: rgba(14, 20, 30, 0.88);
  padding: 12px;
}

.panel-section > summary.panel-title {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-section > summary.panel-title::-webkit-details-marker {
  display: none;
}

.panel-section > summary.panel-title::after {
  content: "v";
  font-size: 0.75rem;
  color: var(--text-dim);
  transition: transform 0.2s ease;
}

.panel-section[open] > summary.panel-title::after {
  transform: rotate(180deg);
}

.panel-title {
  margin: 0 0 8px;
  font-family: var(--font-display);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 0.7rem;
  color: var(--accent);
}

.panel-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  font-size: 0.82rem;
  margin-top: 6px;
}

.muted {
  color: var(--text-dim);
  font-size: 0.82rem;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid rgba(65, 226, 209, 0.35);
  background: rgba(65, 226, 209, 0.16);
  font-size: 0.72rem;
  margin-right: 6px;
  margin-bottom: 6px;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow: auto;
}

.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(125, 211, 255, 0.08);
  cursor: pointer;
  font-size: 0.78rem;
}

.list-item:hover {
  background: rgba(125, 211, 255, 0.16);
}

.range {
  width: 100%;
}

.meta-box {
  margin-top: 8px;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid rgba(125, 170, 220, 0.16);
  background: rgba(9, 14, 22, 0.9);
  font-size: 0.78rem;
  color: var(--text-dim);
  white-space: pre-wrap;
}

.tooltip {
  background: rgba(10, 16, 24, 0.96);
  border: 1px solid rgba(125, 170, 220, 0.2);
  border-radius: 10px;
  padding: 8px 10px;
  color: var(--text);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
  max-width: 240px;
}

.tooltip-title {
  font-size: 0.65rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent-2);
}

.tooltip-sub {
  margin-top: 4px;
  color: var(--text-dim);
  font-size: 0.72rem;
}

.file-input {
  width: 100%;
  padding: 8px;
  border-radius: 10px;
  border: 1px dashed rgba(125, 170, 220, 0.25);
  background: rgba(12, 18, 28, 0.8);
  color: var(--text-dim);
}

.file-input.file-top {
  width: 260px;
  border-style: solid;
}

.purpose-text {
  white-space: pre-wrap;
  font-size: 0.82rem;
  color: var(--text-dim);
  line-height: 1.45;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text);
  margin-top: 6px;
}

.toggle input {
  accent-color: var(--accent);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
  color: var(--text);
  margin-top: 6px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  display: inline-block;
  border: 1px solid rgba(35, 55, 80, 0.18);
}

.swatch.summary {
  background: #7dd3ff;
}

.swatch.takeaways {
  background: #7fe2b1;
}

.swatch.remember {
  background: #ffd166;
}

.swatch.keywords {
  background: #b7f36b;
}

.swatch.raw {
  background: #ffb454;
}

.swatch.anchor {
  background: #ff6b5c;
}

.swatch.line {
  width: 24px;
  height: 4px;
  border-radius: 999px;
}

.swatch.line.semantic {
  background: #41e2d1;
}

.swatch.line.contains {
  background: #ffb454;
}

.swatch.line.time {
  background: #5e6a7a;
}

.swatch.line.tag {
  background: #9fe870;
}

.swatch.line.summary-link {
  background: #4ea5ff;
}

.swatch.line.summary-takeaways {
  background: #7fe2b1;
}

.swatch.line.summary-remember {
  background: #ffd166;
}

.swatch.line.summary-keywords {
  background: #b7f36b;
}

.swatch.line.takeaways-link {
  background: #7fe2b1;
}

.swatch.line.remember-link {
  background: #ffd166;
}

.swatch.line.keywords-link {
  background: #b7f36b;
}

@media (max-width: 980px) {
  .top-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .main {
    grid-template-columns: 1fr;
  }

  .side-panel {
    max-height: none;
  }

  .query-input {
    width: 180px;
  }

  .file-input.file-top {
    width: 100%;
  }
}
