:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --border: #e4e7ec;
  --border-strong: #cbd2dc;
  --text-primary: #1a1d29;
  --text-secondary: #5b6270;
  --text-muted: #8a90a0;
  --accent: #2f6feb;
  --accent-bg: #eaf1ff;
  --radius: 8px;

  --sidebar-bg: #12172b;
  --sidebar-bg-active: #1c2340;
  --sidebar-text: #9aa3bd;
  --sidebar-text-active: #ffffff;
  --sidebar-muted: #5b6280;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text-primary);
}

.app-shell {
  display: none;
  height: 100vh;
  min-height: 0;
}

html.authed .app-shell {
  display: flex;
}

/* Login */

.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  inset: 0;
  height: 100vh;
  background: var(--sidebar-bg);
  z-index: 100;
}

html.authed .login-screen {
  display: none;
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 320px;
  padding: 32px 28px;
  background: var(--card);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.login-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
}

.login-logo i {
  font-size: 20px;
  color: var(--accent);
}

.login-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin: 2px 0 20px;
}

.login-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  margin: 12px 0 6px;
}

.login-input {
  height: 38px;
  padding: 0 12px;
  border: 0.5px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: 14px;
}

.login-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-bg);
  border-color: var(--accent);
}

.login-error {
  font-size: 12px;
  color: #c0372b;
  margin: 10px 0 0;
}

.login-submit {
  margin-top: 20px;
  height: 40px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.login-submit:hover {
  background: #1f56c4;
}

/* Sidebar */

.side-nav {
  flex: 0 0 210px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 16px 10px;
  background: var(--sidebar-bg);
}

.side-nav-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  padding: 8px 10px 18px;
}

.side-nav-logo i {
  font-size: 18px;
  color: var(--accent);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  border: none;
  background: none;
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--sidebar-text);
  cursor: pointer;
}

.nav-item i {
  font-size: 16px;
  width: 18px;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
}

.nav-item.active {
  background: var(--sidebar-bg-active);
  color: var(--sidebar-text-active);
  font-weight: 500;
}

.nav-item.placeholder-nav {
  color: var(--sidebar-muted);
}

/* Top bar */

.shell-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.top-bar {
  height: 56px;
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  background: var(--card);
  border-bottom: 0.5px solid var(--border);
}

.top-search {
  flex: 1;
  max-width: 420px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 0.5px solid var(--border-strong);
  border-radius: 999px;
  padding: 0 14px;
  height: 34px;
}

.top-search i {
  color: var(--text-muted);
  font-size: 15px;
}

.top-search input {
  flex: 1;
  border: none;
  background: none;
  outline: none;
  font-size: 13px;
  color: var(--text-primary);
}

.chain-trace-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  margin-left: auto;
}

.chain-trace-btn:hover {
  background: #1f56c4;
}

.top-bar-icon {
  font-size: 18px;
  color: var(--text-secondary);
  cursor: pointer;
}

.top-bar-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent-bg);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 500;
}

.main-content {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
}

.view {
  display: none;
  height: 100%;
  min-height: 0;
}

.view.active {
  display: flex;
  flex-direction: column;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 0.5px solid var(--border);
}

.view-header h2 {
  font-size: 15px;
  font-weight: 500;
  margin: 0;
}

.view-header input {
  height: 32px;
  padding: 0 10px;
  border: 0.5px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: 13px;
  width: 220px;
}

.view-header input:focus {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-bg);
  border-color: var(--accent);
}

/* Placeholder views */

.view-placeholder.active {
  align-items: center;
  justify-content: center;
}

.placeholder-body {
  text-align: center;
  color: var(--text-muted);
}

.placeholder-body i {
  font-size: 36px;
  margin-bottom: 10px;
  display: block;
}

.placeholder-body h2 {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-secondary);
  margin: 0 0 4px;
}

.placeholder-body p {
  font-size: 13px;
  margin: 0;
}

/* Graph view */

.graph-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px 20px;
  position: relative;
  min-width: 0;
  min-height: 0;
}

.graph-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.graph-toolbar h2 {
  font-size: 15px;
  font-weight: 500;
  margin: 0;
}

#network {
  flex: 1;
  width: 100%;
  min-height: 0;
  border: 0.5px solid var(--border);
  border-radius: 16px;
  background: var(--card);
}

.legend {
  display: flex;
  gap: 16px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
}

.legend-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1.5px solid;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  background: var(--card);
}

.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin: 8px 0 0;
}

.expand-menu {
  position: absolute;
  flex-direction: column;
  gap: 4px;
  background: var(--card);
  border: 0.5px solid var(--border-strong);
  border-radius: 10px;
  padding: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  z-index: 10;
  max-width: 280px;
}

.expand-menu-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin: 0 4px 4px;
}

.expand-menu-detail {
  font-size: 12px;
  color: var(--text-secondary);
  margin: 0 4px 8px;
  line-height: 1.5;
}

.expand-menu-detail a {
  color: var(--accent);
}

.expand-menu button {
  text-align: left;
  border: none;
  background: none;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text-primary);
}

.expand-menu button:hover:not(:disabled) {
  background: var(--accent-bg);
  color: var(--accent);
}

.expand-menu button:disabled {
  color: var(--text-muted);
  cursor: default;
}

.tag {
  font-size: 12px;
  color: var(--text-muted);
}

/* Brands / Products / Chemicals grid views */

.entity-grid {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
  align-content: start;
}

.entity-card {
  background: var(--card);
  border: 0.5px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
}

.entity-card:hover {
  border-color: var(--border-strong);
}

.entity-card img {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  object-fit: cover;
  background: #fff;
}

.entity-card .entity-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.entity-card .entity-name {
  font-size: 13px;
  font-weight: 500;
  margin: 0;
}

.entity-card .entity-sub {
  font-size: 11px;
  color: var(--text-muted);
  margin: 0;
}

/* AI Chat drawer */

.chat-drawer {
  flex: 0 0 20%;
  max-width: 20%;
  display: none;
  flex-direction: column;
  border-left: 0.5px solid var(--border);
  background: var(--card);
  min-height: 0;
}

.chat-drawer.open {
  display: flex;
}

.chat-drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 0.5px solid var(--border);
  font-size: 13px;
  font-weight: 500;
}

.chat-drawer-header span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-drawer-header span i {
  color: var(--accent);
}

.chat-drawer-header button {
  border: none;
  background: none;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
}

.chat-drawer-header button:hover {
  color: var(--text-primary);
}

.chat-drawer-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.drawer-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}

.detail-card {
  padding: 16px;
}

.detail-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 12px;
}

.detail-card-icon {
  font-size: 20px;
}

.detail-card-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
}

.detail-card-type {
  font-size: 11px;
  color: var(--text-secondary);
}

.detail-card-body {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.detail-card-body a {
  color: var(--accent);
}

.chem-structure {
  display: block;
  max-width: 100%;
  margin: 12px auto 0;
  background: #fff;
  border: 0.5px solid var(--border);
  border-radius: var(--radius);
}

.entity-card .chem-structure {
  margin: 0;
}

.detail-card-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 16px;
}

.detail-action-btn {
  text-align: left;
  border: 0.5px solid var(--border-strong);
  background: var(--card);
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 12px;
  cursor: pointer;
  color: var(--text-primary);
}

.detail-action-btn:hover:not(:disabled) {
  background: var(--accent-bg);
  color: var(--accent);
  border-color: var(--accent);
}

.detail-action-btn:disabled {
  color: var(--text-muted);
  cursor: default;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-bubble-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.chat-bubble-row.user {
  flex-direction: row-reverse;
}

.chat-avatar {
  flex: 0 0 24px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.chat-avatar.assistant {
  background: var(--accent-bg);
  color: var(--accent);
}

.chat-avatar.user {
  background: var(--sidebar-bg);
  color: #fff;
}

.chat-bubble {
  max-width: 80%;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
}

.chat-bubble-row.user .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-bubble-row.assistant .chat-bubble {
  background: var(--bg);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.chat-bubble .tag {
  display: block;
  margin-top: 4px;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 0.5px solid var(--border);
}

#chatInput {
  flex: 1;
  height: 38px;
  padding: 0 14px;
  border: 0.5px solid var(--border-strong);
  border-radius: 999px;
  font-size: 13px;
}

#chatInput:focus {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-bg);
  border-color: var(--accent);
}

.chat-input-row button {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex: 0 0 38px;
}

.chat-input-row button:hover {
  background: #1f56c4;
}

/* Mobile */

.nav-toggle-btn {
  display: none;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
}

.nav-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 40;
}

.nav-backdrop.open {
  display: block;
}

@media (max-width: 768px) {
  .nav-toggle-btn {
    display: flex;
  }

  .side-nav {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 240px;
    max-width: 80vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 50;
  }

  .side-nav.open {
    transform: translateX(0);
  }

  .top-bar {
    padding: 0 12px;
    gap: 8px;
  }

  .top-search {
    max-width: none;
  }

  .chain-trace-btn {
    padding: 8px 10px;
    margin-left: 0;
  }

  .view-header {
    flex-wrap: wrap;
    gap: 10px;
    padding: 14px 16px;
  }

  .view-header input {
    width: 100%;
  }

  .entity-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    padding: 14px;
    gap: 10px;
  }

  .graph-panel {
    padding: 12px;
  }

  .graph-toolbar {
    flex-wrap: wrap;
    gap: 8px;
  }

  .graph-toolbar h2 {
    font-size: 14px;
  }

  .chat-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    z-index: 60;
  }
}

@media (max-width: 480px) {
  .top-bar-icon,
  .top-bar-avatar {
    display: none;
  }

  .chain-trace-btn .btn-label {
    display: none;
  }
}
