/* ── Lila Viewer — Design System ──────────────────────────────── */

:root {
  --bg-base: #0a0a0f;
  --bg-card: #111118;
  --bg-elevated: #1a1a24;
  --bg-surface: #222230;
  --bg-hover: #2a2a3a;

  --text-primary: #e8e8f0;
  --text-secondary: #a0a0b8;
  --text-muted: #666680;

  --accent: #7c6cf0;
  --accent-glow: rgba(124, 108, 240, 0.15);
  --green: #34d399;
  --red: #f87171;
  --amber: #fbbf24;
  --blue: #60a5fa;
  --teal: #2dd4bf;

  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);

  --sidebar-width: 300px;
}

/* ── Reset & Base ────────────────────────────────────────────── */

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

html {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-base);
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100vh; }

/* ── Layout ──────────────────────────────────────────────────── */

.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
}

.mobile-header {
  display: none;
}

/* ── Sidebar ─────────────────────────────────────────────────── */

.sidebar {
  background: var(--bg-card);
  border-right: 1px solid var(--bg-surface);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: var(--sp-5);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--bg-surface);
  flex-shrink: 0;
}

.logo {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.02em;
}

/* ── Language Toggle ─────────────────────────────────────────── */

.lang-toggle {
  display: flex;
  gap: 2px;
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.lang-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
  padding: var(--sp-1) var(--sp-3);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}

.lang-btn:hover { color: var(--text-secondary); }
.lang-btn.active {
  background: var(--accent);
  color: white;
}

/* ── Realm List ──────────────────────────────────────────────── */

.realm-list {
  padding: var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  flex-shrink: 0;
}

.realm-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-3) var(--sp-4);
  background: var(--bg-elevated);
  border: 1px solid transparent;
  border-left: 3px solid var(--realm-color, var(--accent));
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
  width: 100%;
  color: var(--text-primary);
  font-family: inherit;
  font-size: inherit;
}

.realm-card:hover {
  background: var(--bg-hover);
  border-color: var(--bg-hover);
}

.realm-card.active {
  background: var(--bg-surface);
  border-color: var(--realm-color, var(--accent));
}

.realm-name {
  font-weight: 600;
  font-size: 0.85rem;
}

.realm-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

/* ── Search ──────────────────────────────────────────────────── */

.search-box {
  padding: var(--sp-3) var(--sp-3) 0;
  flex-shrink: 0;
}

.search-box input {
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  background: var(--bg-elevated);
  border: 1px solid var(--bg-surface);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}

.search-box input:focus {
  border-color: var(--accent);
}

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

/* ── Topic List ──────────────────────────────────────────────── */

.topic-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.topic-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.1s;
  text-align: left;
  width: 100%;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.82rem;
  line-height: 1.4;
}

.topic-item:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.topic-item.active {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.topic-name { flex: 1; }

.topic-score {
  font-size: 0.7rem;
  font-family: 'JetBrains Mono', monospace;
  padding: 1px 5px;
  border-radius: 3px;
  flex-shrink: 0;
}

.score-high { background: rgba(52, 211, 153, 0.15); color: var(--green); }
.score-mid  { background: rgba(251, 191, 36, 0.15); color: var(--amber); }
.score-low  { background: rgba(248, 113, 113, 0.15); color: var(--red); }

/* ── Main Content ────────────────────────────────────────────── */

.main {
  position: relative;
  overflow-y: auto;
  height: 100vh;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  font-size: 1.1rem;
}

.hidden { display: none !important; }

/* ── Loading ─────────────────────────────────────────────────── */

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 15, 0.8);
  z-index: 100;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--bg-surface);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Article View ────────────────────────────────────────────── */

.article-view {
  max-width: 760px;
  margin: 0 auto;
  padding: var(--sp-8) var(--sp-6);
}

.article-header {
  margin-bottom: var(--sp-8);
}

.article-meta {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}

.realm-badge {
  font-size: 0.7rem;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 4px;
  letter-spacing: 0.05em;
}

.realm-ferment  { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }
.realm-freedive { background: rgba(6, 182, 212, 0.15);  color: #06b6d4; }
.realm-stats    { background: rgba(139, 92, 246, 0.15);  color: #8b5cf6; }
.realm-convai   { background: rgba(52, 211, 153, 0.15);  color: #34d399; }

.meta-item {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.score-badge {
  padding: 1px 6px;
  border-radius: 3px;
  background: rgba(52, 211, 153, 0.1);
  color: var(--green);
}

.article-title {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.02em;
}

/* ── Article Sections ────────────────────────────────────────── */

.article-section {
  margin-bottom: var(--sp-8);
}

.article-section h2 {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: var(--sp-3);
}

/* TLDR */
.tldr-section {
  padding: var(--sp-5);
  background: var(--bg-card);
  border-radius: var(--radius-md);
  border-left: 3px solid var(--accent);
}

.tldr-content p {
  font-size: 1.05rem;
  color: var(--text-primary);
  line-height: 1.7;
}

/* Facts */
.facts-content ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.facts-content li {
  padding: var(--sp-2) var(--sp-4);
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  line-height: 1.5;
}

.facts-content li::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--accent);
  border-radius: 50%;
  margin-right: var(--sp-3);
  vertical-align: middle;
}

/* Tags */
.tags-list {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.tag-pill {
  font-size: 0.75rem;
  font-family: 'JetBrains Mono', monospace;
  padding: 2px 10px;
  background: var(--bg-elevated);
  border-radius: 12px;
  color: var(--text-secondary);
}

/* Article body */
.body-content p {
  margin-bottom: 1em;
  line-height: 1.75;
}

.body-content h1 { font-size: 1.3rem; margin: 1.5em 0 0.5em; }
.body-content h2 { font-size: 1.15rem; margin: 1.3em 0 0.5em; }
.body-content h3 { font-size: 1rem; margin: 1em 0 0.4em; }

.body-content ul,
.body-content ol {
  margin: 0.5em 0 1em 1.5em;
}

.body-content li {
  margin-bottom: 0.3em;
  line-height: 1.6;
}

.body-content pre {
  background: var(--bg-card);
  padding: var(--sp-4);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 1em 0;
}

.body-content code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
  background: var(--bg-elevated);
  padding: 1px 5px;
  border-radius: 3px;
}

.body-content pre code {
  background: none;
  padding: 0;
}

.body-content strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* ── Semantic Link Pills ─────────────────────────────────────── */

.link-pill {
  display: inline-block;
  font-size: 0.8rem;
  font-family: 'JetBrains Mono', monospace;
  padding: 1px 8px;
  border-radius: 10px;
  margin: 1px;
  text-decoration: none;
  transition: all 0.15s;
  white-space: nowrap;
}

.link-pill.resolvable {
  background: rgba(96, 165, 250, 0.12);
  color: var(--blue);
  cursor: pointer;
}

.link-pill.resolvable:hover {
  background: rgba(96, 165, 250, 0.25);
}

.link-pill.unresolved {
  background: var(--bg-elevated);
  color: var(--text-muted);
}

.link-pill.foreign {
  background: rgba(251, 191, 36, 0.10);
  color: var(--amber);
  cursor: help;
  border-bottom: 1px dotted var(--amber);
}

.link-pill.foreign:hover {
  background: rgba(251, 191, 36, 0.20);
}

/* Foreign tooltip (uses title attribute, enhanced with CSS) */
.link-pill.foreign:hover::after {
  content: attr(title);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--bg-surface);
  color: var(--text-primary);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.75rem;
  font-family: 'Inter', sans-serif;
  max-width: 300px;
  white-space: normal;
  z-index: 100;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.link-pill.foreign {
  position: relative;
}

.link-domain {
  font-size: 0.7em;
  opacity: 0.5;
}

/* Links section grid */
.links-grid {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}

.links-section .link-pill {
  font-size: 0.78rem;
  padding: 3px 10px;
}

/* ── KaTeX overrides ─────────────────────────────────────────── */

.katex-display {
  margin: 1em 0;
  overflow-x: auto;
  padding: var(--sp-2) 0;
}

.katex { color: var(--text-primary); }

/* ── Scrollbar ───────────────────────────────────────────────── */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-surface); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--bg-hover); }

/* ── Mobile ──────────────────────────────────────────────────── */

@media (max-width: 768px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  .mobile-header {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    background: var(--bg-card);
    border-bottom: 1px solid var(--bg-surface);
    position: sticky;
    top: 0;
    z-index: 50;
  }

  .sidebar-toggle {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: var(--sp-1);
  }

  .mobile-title {
    font-weight: 700;
    color: var(--accent);
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: var(--sidebar-width);
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-md);
  }

  .main { height: auto; min-height: 100vh; }

  .article-view {
    padding: var(--sp-4);
  }

  .article-title {
    font-size: 1.3rem;
  }
}
