:root {
  --bg: #080810;
  --bg-secondary: #0c0c18;
  --text-primary: #e8e8e8;
  --text-secondary: #888;
  --text-tertiary: #666;
  --accent: #00d2b4;
  --accent-subtle: rgba(0, 210, 180, 0.06);
  --accent-border: rgba(0, 210, 180, 0.3);
  --purple: #c77dff;
  --amber: #e6a919;
  --red: #ff4444;
  --surface: rgba(255, 255, 255, 0.03);
  --divider: rgba(255, 255, 255, 0.05);
  --accent-hover: rgba(0, 210, 180, 0.12);
  --accent-selected: rgba(0, 210, 180, 0.15);
  --fab-glow: rgba(0, 210, 180, 0.3);
  --send-hover: #00e6c4;
  --insight-stroke: rgba(230, 169, 25, 0.5);
  --constellation-bg: #060610;
  --node-link: rgba(0, 210, 180, 0.06);
  --node-glow: rgba(0, 210, 180, 0.1);
  --node-fill: rgba(0, 210, 180, 0.08);
  --node-border: rgba(0, 210, 180, 0.3);
  --node-text: #00d2b4;
  --node-voices: rgba(102, 102, 102, 0.7);
}

[data-theme="light"] {
  --bg: #f5f5f0;
  --bg-secondary: #ebebe6;
  --text-primary: #1a1a2e;
  --text-secondary: #4a4a6a;
  --text-tertiary: #7070a0;
  --accent: #008870;
  --accent-subtle: rgba(0, 136, 112, 0.08);
  --accent-border: rgba(0, 136, 112, 0.25);
  --purple: #8844cc;
  --amber: #b87a00;
  --red: #cc2020;
  --surface: rgba(0, 0, 0, 0.04);
  --divider: rgba(0, 0, 0, 0.08);
  --accent-hover: rgba(0, 136, 112, 0.12);
  --accent-selected: rgba(0, 136, 112, 0.15);
  --fab-glow: rgba(0, 136, 112, 0.35);
  --send-hover: #009c80;
  --insight-stroke: rgba(184, 122, 0, 0.5);
  --constellation-bg: #f0f0ea;
  --node-link: rgba(0, 136, 112, 0.12);
  --node-glow: rgba(0, 136, 112, 0.2);
  --node-fill: rgba(0, 136, 112, 0.1);
  --node-border: rgba(0, 136, 112, 0.35);
  --node-text: #008870;
  --node-voices: rgba(80, 80, 120, 0.8);
}

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

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  font-size: 16px;
  overflow: hidden;
  height: 100vh;
}

/* Views */
.view {
  display: none;
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
}
.view.active { display: flex; flex-direction: column; }

/* Constellation */
#constellation-view {
  position: relative;
}
#constellation-graph {
  width: 100%;
  height: 100%;
}
#constellation-graph svg {
  width: 100%;
  height: 100%;
}
#constellation-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
}
#constellation-hint {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--text-tertiary);
  font-size: 12px;
  font-style: italic;
}

/* Hover Preview */
#hover-preview {
  position: absolute;
  background: var(--bg-secondary);
  border: 1px solid var(--accent-border);
  border-radius: 8px;
  padding: 16px;
  max-width: 280px;
  pointer-events: none;
  z-index: 10;
}
#hover-preview.hidden { display: none; }
#hover-preview .preview-hook {
  color: var(--accent);
  font-style: italic;
  font-size: 14px;
  margin-bottom: 8px;
}
#hover-preview .preview-meta {
  color: var(--text-tertiary);
  font-size: 11px;
}
#hover-preview .preview-threads {
  margin-top: 8px;
  border-top: 1px solid var(--divider);
  padding-top: 8px;
}
#hover-preview .preview-thread {
  border-left: 2px solid var(--purple);
  padding-left: 8px;
  margin-bottom: 6px;
  color: var(--text-secondary);
  font-size: 12px;
  font-style: italic;
}

/* FAB */
.fab {
  position: absolute;
  bottom: 30px;
  right: 30px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--bg);
  font-size: 28px;
  border: none;
  cursor: pointer;
  z-index: 20;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.fab:hover {
  transform: scale(1.1);
  box-shadow: 0 0 20px var(--fab-glow);
}

/* Back button */
.back-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  padding: 16px 24px;
  align-self: flex-start;
  transition: color 0.15s ease;
}
.back-btn:hover { color: var(--accent); }

/* Topic View */
#topic-header {
  padding: 0 24px 24px;
}
#topic-header h2 {
  color: var(--text-primary);
  font-weight: 300;
  font-size: clamp(1.5rem, 3vw, 2rem);
  font-style: italic;
}
#topic-header .topic-meta {
  color: var(--text-tertiary);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-top: 8px;
}
#topic-threads {
  flex: 1;
  overflow-y: auto;
  padding: 0 24px;
}

/* Thread cards */
.thread-card {
  background: var(--surface);
  border: 1px solid var(--text-tertiary);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.thread-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}
.thread-card .thread-hook {
  color: var(--text-primary);
  font-style: italic;
  margin-bottom: 8px;
}
.thread-card .thread-meta {
  color: var(--text-tertiary);
  font-size: 12px;
}

/* Thread View */
#thread-header {
  padding: 0 24px;
}
#thread-header h3 {
  color: var(--text-primary);
  font-weight: 300;
  font-style: italic;
}
#thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 24px;
}

/* Messages */
.message {
  margin-bottom: 16px;
  padding-left: 12px;
  border-left: 2px solid var(--text-tertiary);
}
.message.visitor { border-left-color: var(--accent); }
.message.ai { border-left-color: var(--amber); }
.message .message-author {
  font-size: 11px;
  margin-bottom: 4px;
}
.message.visitor .message-author { color: var(--accent); }
.message.ai .message-author { color: var(--amber); }
.message .message-time {
  font-size: 10px;
  color: var(--text-tertiary);
  margin-left: 8px;
}
.message .message-content {
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.6;
}

/* Synthesis button */
.synthesis-btn {
  background: var(--accent-subtle);
  border: 1px solid var(--accent-border);
  color: var(--accent);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  margin: 8px 24px;
  transition: background 0.15s ease;
}
.synthesis-btn:hover {
  background: var(--accent-hover);
}

/* Thread Input */
#thread-input {
  padding: 16px 24px;
  border-top: 1px solid var(--divider);
  background: var(--bg-secondary);
}
#message-input-row {
  display: flex;
  gap: 8px;
}
#message-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--text-tertiary);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
}
#message-input:focus { border-color: var(--accent); }
#send-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}
#send-btn:hover { background: var(--send-hover); }
#branch-btn {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--text-tertiary);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}
#branch-btn:hover { color: var(--purple); border-color: var(--purple); }

#display-name-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--text-tertiary);
  border-radius: 8px;
  padding: 8px 14px;
  color: var(--text-primary);
  font-size: 13px;
  margin-bottom: 8px;
  outline: none;
}

/* Modal */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--accent-border);
  border-radius: 16px;
  padding: 32px;
  max-width: 560px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-tertiary);
  font-size: 24px;
  cursor: pointer;
}
.modal-content h2 {
  font-weight: 300;
  margin-bottom: 24px;
  color: var(--accent);
}
.modal-content label {
  display: block;
  color: var(--text-secondary);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
}
.modal-content textarea, .modal-content input[type="text"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--text-tertiary);
  border-radius: 8px;
  padding: 12px;
  color: var(--text-primary);
  font-size: 14px;
  resize: vertical;
  outline: none;
  margin-bottom: 16px;
}
.modal-content textarea:focus, .modal-content input:focus {
  border-color: var(--accent);
}

/* Hook options */
.hook-option {
  background: var(--accent-subtle);
  border: 1px solid var(--accent-border);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  color: var(--accent);
  font-style: italic;
  font-size: 14px;
  transition: background 0.15s ease;
}
.hook-option:hover { background: var(--accent-hover); }
.hook-option.selected { border-color: var(--accent); background: var(--accent-selected); }

/* Image options */
#image-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.image-option {
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s ease;
  aspect-ratio: 1;
}
.image-option:hover { border-color: var(--accent-border); }
.image-option.selected { border-color: var(--accent); }
.image-option img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.image-option .image-credit {
  font-size: 9px;
  color: var(--text-tertiary);
  padding: 4px 8px;
  background: var(--bg);
}

/* Buttons */
#sharpen-btn, #publish-topic-btn {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}

/* Insight diamonds */
.insight-node {
  fill: var(--amber);
  stroke: var(--insight-stroke);
}

/* Scrollbars */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-tertiary); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }
* { scrollbar-width: thin; scrollbar-color: var(--text-tertiary) transparent; }

/* Theme toggle button */
#theme-toggle-btn {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 200;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-secondary);
  border: 1px solid var(--accent-border);
  color: var(--text-primary);
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
#theme-toggle-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent);
}

/* Constellation SVG — CSS overrides for D3 presentation attributes */
.constellation-bg { fill: var(--constellation-bg); }
.topic-link { stroke: var(--node-link); }
.glow { fill: none; stroke: var(--node-glow); }
.node-bg { fill: var(--node-fill); }
.node-border { fill: none; stroke: var(--node-border); }
.node-hook { fill: var(--node-text); }
.node-voices { fill: var(--node-voices); }

.hidden { display: none !important; }

/* Mobile */
@media (max-width: 900px) {
  .fab { bottom: 20px; right: 20px; }
  #topic-header, #thread-messages, #thread-input { padding-left: 16px; padding-right: 16px; }
  #image-options { grid-template-columns: 1fr; }
}

/* Search */
#search-bar {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 150;
  display: flex;
  align-items: center;
  background: var(--bg-secondary);
  border: 1px solid var(--accent-border);
  border-radius: 20px;
  padding: 6px 14px;
  width: min(360px, calc(100vw - 160px));
  gap: 8px;
}
#search-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 14px;
}
#search-input::placeholder { color: var(--text-tertiary); }
#search-dropdown {
  position: fixed;
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 150;
  background: var(--bg-secondary);
  border: 1px solid var(--accent-border);
  border-radius: 10px;
  width: min(360px, calc(100vw - 160px));
  max-height: 320px;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.search-result {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--divider);
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--accent-hover); }
.search-result-type {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  flex-shrink: 0;
}
.search-result-hook {
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-empty {
  padding: 12px 14px;
  color: var(--text-tertiary);
  font-size: 13px;
  text-align: center;
}
.search-loading {
  padding: 12px 14px;
  color: var(--text-tertiary);
  font-size: 13px;
  text-align: center;
  font-style: italic;
}

/* Toast notifications */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--bg-secondary);
  border: 1px solid var(--divider);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  animation: toast-in 0.2s ease;
  pointer-events: auto;
}
.toast-info  { border-left: 3px solid var(--accent); }
.toast-success { border-left: 3px solid #4caf50; }
.toast-error { border-left: 3px solid var(--red); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Unread badge on thread cards */
.unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--bg);
  font-size: 11px;
  font-weight: 600;
  margin-left: 8px;
  line-height: 1;
}

/* Insights Panel */
.insights-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 260px;
  height: 100vh;
  background: var(--bg-secondary);
  border-left: 1px solid var(--divider);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
}
.insights-panel.collapsed {
  transform: translateX(260px);
}
.insights-toggle {
  position: absolute;
  left: -38px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--bg-secondary);
  border: 1px solid var(--divider);
  border-right: none;
  color: var(--text-secondary);
  width: 38px;
  height: 88px;
  cursor: pointer;
  border-radius: 8px 0 0 8px;
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  transition: color 0.15s ease;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  padding: 0;
}
.insights-toggle:hover {
  color: var(--amber);
}
.insights-panel-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
}
.insights-panel-title {
  color: var(--amber);
  font-size: 11px;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 14px;
}
.insight-card {
  background: var(--surface);
  border: 1px solid var(--insight-stroke);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.insight-header {
  color: var(--text-primary);
  font-size: 13px;
  font-style: italic;
  line-height: 1.4;
  margin-bottom: 6px;
}
.insight-badge {
  display: inline-block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 2px 6px;
  border-radius: 4px;
}
.insight-badge.consensus {
  background: var(--accent-subtle);
  color: var(--accent);
  border: 1px solid var(--accent-border);
}
.insight-badge.tension {
  background: rgba(255, 68, 68, 0.08);
  color: var(--red);
  border: 1px solid rgba(255, 68, 68, 0.25);
}
.insight-badge.emerging_theme {
  background: rgba(199, 125, 255, 0.08);
  color: var(--purple);
  border: 1px solid rgba(199, 125, 255, 0.25);
}
.insights-empty {
  color: var(--text-tertiary);
  font-size: 13px;
  font-style: italic;
  text-align: center;
  margin-top: 24px;
}
