@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,600;0,700;1,400&family=Inter:wght@300;400;500;600&display=swap');

:root {
  --bg-primary: #0f1117;
  --bg-secondary: #181b25;
  --bg-card: #1e2230;
  --bg-card-hover: #252a3a;
  --bg-input: #252a3a;
  --border: #2a3040;
  --border-light: #353d52;
  --text-primary: #e8e6e1;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --gold: #C6A44E;
  --gold-dim: rgba(198,164,78,0.15);
  --copper: #B87333;
  --copper-dim: rgba(184,115,51,0.15);
  --green: #5B8A72;
  --green-dim: rgba(91,138,114,0.15);
  --purple: #8B6DAE;
  --purple-dim: rgba(139,109,174,0.15);
  --blue: #5B89B5;
  --blue-dim: rgba(91,137,181,0.15);
  --red: #B85B5B;
  --red-dim: rgba(184,91,91,0.15);
  --character-color: #C6A44E;
  --place-color: #5B8A72;
  --thing-color: #8B6DAE;
  --event-color: #5B89B5;
  --divergence-color: #B85B5B;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
}

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

body {
  font-family: 'Inter', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.6;
}

h1, h2, h3, h4 {
  font-family: 'Cormorant Garamond', serif;
  font-weight: 600;
  letter-spacing: 0.01em;
}

/* ── Header ─────────────────────────────── */
.header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 2rem;
  display: flex;
  align-items: center;
  height: 64px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-title {
  font-family: 'Cormorant Garamond', serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--gold);
  margin-right: 2rem;
  white-space: nowrap;
}

.header-title span {
  color: var(--text-muted);
  font-weight: 400;
  font-size: 0.9rem;
  margin-left: 0.5rem;
  font-family: 'Inter', sans-serif;
}

.nav {
  display: flex;
  gap: 0.25rem;
  flex: 1;
}

.nav-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.85rem;
  font-family: 'Inter', sans-serif;
  transition: var(--transition);
  white-space: nowrap;
}

.nav-btn:hover { background: var(--bg-card); color: var(--text-primary); }
.nav-btn.active { background: var(--gold-dim); color: var(--gold); }

.search-bar {
  margin-left: auto;
  position: relative;
}

.search-bar input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.4rem 1rem 0.4rem 2.2rem;
  color: var(--text-primary);
  width: 260px;
  font-size: 0.85rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  transition: var(--transition);
}

.search-bar input:focus {
  border-color: var(--gold);
  width: 320px;
}

.search-bar::before {
  content: '\1F50D';
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.8rem;
  opacity: 0.5;
}

/* ── Main Layout ─────────────────────────── */
.main { padding: 1.5rem 2rem; max-width: 1600px; margin: 0 auto; }

.view { display: none; }
.view.active { display: block; }

/* ── Cards & Grid ─────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  transition: var(--transition);
}

.stat-card:hover {
  border-color: var(--border-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.stat-card.clickable { cursor: pointer; }

.stat-card .number {
  font-family: 'Cormorant Garamond', serif;
  font-size: 2.2rem;
  font-weight: 700;
  line-height: 1;
  margin-bottom: 0.25rem;
}

.stat-card .label {
  color: var(--text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.stat-card.character .number { color: var(--character-color); }
.stat-card.place .number { color: var(--place-color); }
.stat-card.thing .number { color: var(--thing-color); }
.stat-card.event .number { color: var(--event-color); }
.stat-card.divergence .number { color: var(--divergence-color); }

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.chart-card h3 {
  color: var(--text-secondary);
  font-size: 1rem;
  margin-bottom: 1rem;
  font-family: 'Inter', sans-serif;
  font-weight: 500;
}

/* ── Entity List ─────────────────────────── */
.entity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
}

.entity-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
}

.entity-card::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
}

.entity-card.character::before { background: var(--character-color); }
.entity-card.place::before { background: var(--place-color); }
.entity-card.thing::before { background: var(--thing-color); }

.entity-card:hover {
  border-color: var(--border-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* Card thumbnail */
.card-thumb {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--border-light);
  opacity: 0.85;
  transition: opacity 0.2s, transform 0.2s;
}
.entity-card:hover .card-thumb {
  opacity: 1;
  transform: scale(1.05);
}

.entity-card h4 {
  font-size: 1.15rem;
  margin-bottom: 0.35rem;
  font-family: 'Cormorant Garamond', serif;
  padding-right: 56px;
}

.entity-card .meta {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-bottom: 0.5rem;
}

.entity-card .desc {
  color: var(--text-secondary);
  font-size: 0.85rem;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.type-badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 12px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 500;
}

.type-badge.character { background: var(--gold-dim); color: var(--gold); }
.type-badge.place { background: var(--green-dim); color: var(--green); }
.type-badge.thing { background: var(--purple-dim); color: var(--purple); }
.type-badge.event { background: var(--blue-dim); color: var(--blue); }

/* ── Entity Detail Panel ─────────────────── */
/* Detail hero image */
.detail-hero {
  margin: -2rem -2rem 1.5rem -2rem;
  border-radius: var(--radius) var(--radius) 0 0;
  overflow: hidden;
  max-height: 450px;
  background: var(--bg-card);
}
.detail-hero-img {
  width: 100%;
  max-height: 450px;
  object-fit: cover;
  object-position: center 20%;
  display: block;
}

.detail-panel {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  margin-bottom: 2rem;
}

.detail-panel h2 {
  font-size: 2rem;
  margin-bottom: 0.25rem;
}

.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.detail-header .meta {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.detail-section {
  margin-bottom: 1.5rem;
}

.detail-section h3 {
  font-family: 'Inter', sans-serif;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.description-quote {
  background: var(--bg-card);
  border-left: 3px solid var(--copper);
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-style: italic;
  color: var(--text-secondary);
}

.description-quote .sense-tag {
  display: inline-block;
  font-style: normal;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  margin-right: 0.5rem;
}

.sense-tag.visual { background: var(--blue-dim); color: var(--blue); }
.sense-tag.sound { background: var(--green-dim); color: var(--green); }
.sense-tag.smell { background: var(--copper-dim); color: var(--copper); }
.sense-tag.taste { background: var(--red-dim); color: var(--red); }
.sense-tag.touch { background: var(--purple-dim); color: var(--purple); }
.sense-tag.emotional { background: var(--gold-dim); color: var(--gold); }
.sense-tag.atmosphere { background: rgba(100,120,140,0.15); color: #7a8a9a; }

.relationship-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.rel-chip {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.3rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.rel-chip:hover { border-color: var(--gold); }

.rel-chip .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.rel-chip .rel-type {
  color: var(--text-muted);
  font-size: 0.7rem;
}

/* ── Timeline ─────────────────────────── */
.timeline-container {
  position: relative;
  padding-left: 3rem;
}

.timeline-container::before {
  content: '';
  position: absolute;
  left: 1rem;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--border);
}

.timeline-event {
  position: relative;
  margin-bottom: 1.5rem;
  padding: 1rem 1.25rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.timeline-event:hover {
  border-color: var(--border-light);
  box-shadow: var(--shadow);
}

.timeline-event::before {
  content: '';
  position: absolute;
  left: -2.35rem;
  top: 1.25rem;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--event-color);
  border: 2px solid var(--bg-primary);
}

.timeline-event.divergence::before { background: var(--divergence-color); }

.timeline-event .year {
  font-family: 'Cormorant Garamond', serif;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--gold);
  margin-bottom: 0.25rem;
}

.timeline-event h4 {
  font-size: 1rem;
  margin-bottom: 0.35rem;
}

.timeline-event .event-meta {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-bottom: 0.35rem;
}

.timeline-event .event-desc {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.timeline-event .entity-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.5rem;
}

.timeline-event .entity-tag {
  font-size: 0.7rem;
  padding: 0.1rem 0.5rem;
  border-radius: 10px;
  cursor: pointer;
}

/* ── Graph ─────────────────────────── */
#graph-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
}

#graph-container svg {
  width: 100%;
  height: 600px;
}

.graph-legend {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(15,17,23,0.9);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
}

.graph-legend-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.35rem;
}

.graph-legend-item .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

/* ── Filter Bar ─────────────────────────── */
.filter-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  align-items: center;
}

.filter-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 0.4rem 0.9rem;
  border-radius: 20px;
  cursor: pointer;
  font-size: 0.8rem;
  font-family: 'Inter', sans-serif;
  transition: var(--transition);
}

.filter-btn:hover { border-color: var(--border-light); }
.filter-btn.active { border-color: var(--gold); color: var(--gold); background: var(--gold-dim); }

/* ── Forms ─────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.modal-overlay.active { opacity: 1; pointer-events: all; }

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 90%;
  max-width: 600px;
  max-height: 85vh;
  overflow-y: auto;
}

.modal h2 {
  margin-bottom: 1.5rem;
  font-size: 1.5rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  color: var(--text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.35rem;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  color: var(--text-primary);
  font-size: 0.9rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  transition: var(--transition);
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--gold);
}

.form-group textarea { min-height: 80px; resize: vertical; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.btn {
  padding: 0.5rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.85rem;
  font-family: 'Inter', sans-serif;
  font-weight: 500;
  transition: var(--transition);
}

.btn-primary { background: var(--gold); color: #0f1117; }
.btn-primary:hover { background: #d4b05e; }
.btn-secondary { background: var(--bg-card); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--border-light); color: var(--text-primary); }
.btn-danger { background: var(--red-dim); color: var(--red); }
.btn-danger:hover { background: rgba(184,91,91,0.25); }

.btn-group { display: flex; gap: 0.5rem; margin-top: 1.5rem; }

.btn-floating {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--gold);
  color: #0f1117;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(198,164,78,0.4);
  transition: var(--transition);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-floating:hover { transform: scale(1.1); }

/* ── Search Results ─────────────────────── */
.search-results {
  position: absolute;
  top: 100%;
  right: 0;
  width: 400px;
  max-height: 500px;
  overflow-y: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 150;
  display: none;
}

.search-results.active { display: block; }

.search-group h4 {
  padding: 0.5rem 1rem;
  color: var(--text-muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-family: 'Inter', sans-serif;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.search-item {
  padding: 0.5rem 1rem;
  cursor: pointer;
  transition: var(--transition);
  border-bottom: 1px solid var(--border);
}

.search-item:hover { background: var(--bg-card); }

.search-item .name { font-weight: 500; }
.search-item .meta { color: var(--text-muted); font-size: 0.8rem; }

/* ── Chapter List ─────────────────────── */
.chapter-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.chapter-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  display: grid;
  grid-template-columns: 60px 1fr 200px 120px;
  align-items: center;
  gap: 1rem;
  cursor: pointer;
  transition: var(--transition);
}

.chapter-row:hover {
  border-color: var(--border-light);
  transform: translateY(-1px);
}

.chapter-row .ch-num {
  font-family: 'Cormorant Garamond', serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--gold);
}

.chapter-row .ch-info h4 { font-size: 1.05rem; margin-bottom: 0.2rem; }
.chapter-row .ch-info .summary { color: var(--text-muted); font-size: 0.8rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.chapter-row .ch-period { color: var(--text-secondary); font-size: 0.85rem; }
.chapter-row .ch-pov { color: var(--text-muted); font-size: 0.8rem; }

/* ── Back Button ─────────────────────── */
.back-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  font-family: 'Inter', sans-serif;
  margin-bottom: 1rem;
  padding: 0.25rem 0;
  transition: var(--transition);
}

.back-btn:hover { color: var(--gold); }

/* ── Scrollbar ─────────────────────── */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* ── Responsive ─────────────────────── */
@media (max-width: 768px) {
  .header {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
    height: auto;
    gap: 0.5rem;
  }
  .header-title {
    width: 100%;
    margin-right: 0;
    font-size: 1.25rem;
    text-align: center;
  }
  .header-title span { display: block; margin-left: 0; margin-top: 0.1rem; }
  .search-bar {
    order: 2;
    width: 100%;
    margin-left: 0;
  }
  .search-bar input { width: 100%; box-sizing: border-box; }
  .search-bar input:focus { width: 100%; }
  .nav {
    order: 3;
    width: 100%;
    overflow-x: auto;
    padding-bottom: 0.25rem;
    justify-content: center;
    -webkit-overflow-scrolling: touch;
  }
  .nav-btn { padding: 0.4rem 0.65rem; font-size: 0.75rem; }
  .main { padding: 1rem; }
  .charts-grid { grid-template-columns: 1fr; }
  .chapter-row { grid-template-columns: 50px 1fr; }
  .chapter-row .ch-period, .chapter-row .ch-pov { display: none; }
  .stats-grid { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
  .search-results { width: calc(100vw - 2rem); left: 1rem; right: 1rem; }
  .drilldown-panel { width: 90vw; right: 5vw; }
  .detail-hero { margin: -1.5rem -1.5rem 1rem -1.5rem; }
  .detail-panel { padding: 1.5rem; }
}

/* ── Chart Drilldown Panel ─────────────── */
#chart-drilldown { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; z-index: 1000; }
.drilldown-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); backdrop-filter: blur(2px); }
.drilldown-panel {
  position: absolute; top: 50%; right: 2rem; transform: translateY(-50%);
  width: 380px; max-height: 80vh; background: var(--bg-card); border: 1px solid var(--border-light);
  border-radius: 12px; overflow: hidden; display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5); animation: drilldownSlide 0.2s ease-out;
}
@keyframes drilldownSlide { from { opacity: 0; transform: translateY(-50%) translateX(20px); } to { opacity: 1; transform: translateY(-50%) translateX(0); } }
.drilldown-header-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem; border-bottom: 1px solid var(--border);
}
.drilldown-header-bar h3 {
  margin: 0; font-family: 'Cormorant Garamond', serif; font-size: 1.15rem;
  font-weight: 700; color: var(--gold);
}
.drilldown-close {
  background: none; border: none; color: var(--text-muted); font-size: 1.5rem;
  cursor: pointer; padding: 0; line-height: 1;
}
.drilldown-close:hover { color: var(--text-primary); }
.drilldown-body { padding: 0.5rem; overflow-y: auto; max-height: calc(80vh - 60px); }
.drilldown-item {
  padding: 0.6rem 0.75rem; border-radius: 6px; cursor: pointer;
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  transition: background 0.15s;
}
.drilldown-item:hover { background: var(--bg-card-hover); }
.drilldown-label { color: var(--text-primary); font-size: 0.85rem; font-weight: 500; }
.drilldown-meta { color: var(--text-muted); font-size: 0.7rem; white-space: nowrap; }

/* ── Notable Quotes ────────────────────── */
.notable-quote {
  border-left: 3px solid var(--gold);
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  background: var(--gold-dim);
  border-radius: 0 8px 8px 0;
}
.notable-quote .quote-text {
  color: var(--text-primary);
  font-family: 'Cormorant Garamond', serif;
  font-size: 1.1rem;
  font-style: italic;
  font-weight: 600;
  line-height: 1.6;
}
.notable-quote .quote-meta {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-top: 0.35rem;
  font-style: normal;
}
