:root {
  --bg: #0f1117;
  --bg-panel: #161b22;
  --bg-card: #1c2333;
  --bg-hover: #243044;
  --border: #2d3748;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #3b82f6;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --gray: #64748b;
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

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

/* Header */
.header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-left {
  justify-self: start;
}

.header-left h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-left: 0.75rem;
}

.header-center {
  justify-self: center;
  width: min(520px, 55vw);
}

#search {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.65rem 1.15rem;
  border-radius: 10px;
  width: 100%;
  font-size: 0.95rem;
}

#search:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.header-right {
  justify-self: end;
}

/* Sectors */
.sectors {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.sector-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 0.4rem 1rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}

.sector-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.sector-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* Main layout */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Layers panel */
.layers-panel {
  width: 280px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.btn-text {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  cursor: pointer;
}

.layers-list {
  overflow-y: auto;
  padding: 0.5rem;
  flex: 1;
}

.layer-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--text-muted);
  transition: all 0.12s;
}

.layer-names {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
  flex: 1;
}

.layer-cz {
  font-weight: 500;
  line-height: 1.3;
}

.layer-en {
  font-size: 0.68rem;
  opacity: 0.7;
  line-height: 1.2;
}

.layer-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.layer-item.active {
  background: rgba(59, 130, 246, 0.15);
  color: var(--accent);
  font-weight: 500;
}

.layer-item .count {
  margin-left: auto;
  font-size: 0.75rem;
  background: var(--bg);
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Content */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
}

.content-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.content-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.legend {
  display: flex;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

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

.dot.green { background: var(--green); }
.dot.yellow { background: var(--yellow); }
.dot.red { background: var(--red); }
.dot.gray { background: var(--gray); }

/* Companies grid */
.companies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.company-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.1rem;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
  overflow: hidden;
}

.company-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
}

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

.company-card.micro::before { background: var(--green); }
.company-card.mid::before { background: var(--yellow); }
.company-card.large::before { background: var(--red); }
.company-card.unknown::before { background: var(--gray); }

.company-name {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 0.15rem;
}

.company-ticker {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-family: ui-monospace, monospace;
}

.company-mc {
  margin-top: 0.6rem;
  font-size: 0.9rem;
  font-weight: 500;
}

.company-layers {
  margin-top: 0.75rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.tag {
  font-size: 0.7rem;
  background: rgba(59, 130, 246, 0.12);
  color: #93c5fd;
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  max-width: 520px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 1.75rem;
  z-index: 1;
}

.modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
}

.modal-close:hover {
  color: var(--text);
}

.modal-body h2 {
  font-size: 1.4rem;
  margin-bottom: 0.25rem;
}

.modal-meta {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.25rem;
}

.modal-section {
  margin-top: 1.25rem;
}

.modal-section h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
}

/* Responsive */
@media (max-width: 800px) {
  .layers-panel {
    display: none;
  }
  #search {
    width: 160px;
  }
}

/* Relationship badges on cards */
.rel-badges {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.65rem;
}

.rel-badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
}

.rel-badge.supply {
  background: rgba(34, 197, 94, 0.15);
  color: #4ade80;
}

.rel-badge.buy {
  background: rgba(59, 130, 246, 0.15);
  color: #93c5fd;
}

/* Modal relations */
.rel-group {
  margin-bottom: 1rem;
}

.rel-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.rel-list {
  list-style: none;
  padding-left: 0;
}

.rel-list li {
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.rel-list li:last-child {
  border-bottom: none;
}

.rel-source {
  font-size: 0.75rem;
  color: var(--text-muted);
}

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

/* Chinese company label */
.company-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.cn-badge {
  flex-shrink: 0;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: rgba(249, 115, 22, 0.2);
  color: #fb923c;
  border: 1px solid rgba(249, 115, 22, 0.4);
  padding: 0.12rem 0.4rem;
  border-radius: 4px;
  line-height: 1.3;
}

.company-card.chinese {
  border-color: rgba(249, 115, 22, 0.25);
}

.company-card.chinese:hover {
  border-color: #f97316;
}

/* Documents gallery in modal */
.docs-grid {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 0.5rem;
}

.doc-card {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 0.85rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  align-items: start;
}

.doc-thumb-link {
  display: block;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.doc-thumb {
  width: 140px;
  height: 100px;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.doc-thumb.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  padding: 0.5rem;
}

.doc-title {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.25rem;
  line-height: 1.3;
}

.doc-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.doc-source {
  color: #fb923c;
  font-weight: 500;
}

.doc-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: 0.35rem;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.doc-link {
  font-size: 0.8rem;
  color: #93c5fd;
  text-decoration: none;
}

.doc-link:hover {
  text-decoration: underline;
}

.modal-content {
  max-width: 640px;
}

@media (max-width: 560px) {
  .doc-card {
    grid-template-columns: 1fr;
  }
  .doc-thumb, .doc-thumb.placeholder {
    width: 100%;
    height: 140px;
  }
}

/* Clickable tags & company links in modal */
.tag-link {
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}

.tag-link:hover {
  background: rgba(59, 130, 246, 0.35);
  color: #bfdbfe;
}

.sector-tag {
  background: rgba(168, 85, 247, 0.15);
  color: #d8b4fe;
}

.sector-tag:hover {
  background: rgba(168, 85, 247, 0.35);
  color: #e9d5ff;
}

.company-link {
  cursor: pointer;
  color: #93c5fd;
  border-bottom: 1px dashed rgba(147, 197, 253, 0.4);
}

.company-link:hover {
  color: #bfdbfe;
  border-bottom-color: #bfdbfe;
}
