:root {
  --bg-top: #f3efe4;
  --bg-bottom: #dae7ee;
  --surface: rgba(254, 252, 246, 0.92);
  --surface-strong: #fffdf8;
  --border: rgba(35, 55, 67, 0.14);
  --ink: #15323d;
  --muted: #556f7a;
  --shadow: 0 18px 40px rgba(28, 44, 54, 0.14);
  --chemical: #ff4fa5;
  --reaction: #00bbd4;
  --pathway-chemical: #ff7a59;
  --pathway-reaction: #5fd0ac;
  --pathway-line: #2a5b83;
  --button-dark: #173848;
  --button-dark-hover: #102734;
  --button-soft: #eef3f5;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(255, 127, 80, 0.18), transparent 28%),
    radial-gradient(circle at top right, rgba(0, 187, 212, 0.16), transparent 30%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  font-family: "Trebuchet MS", "Gill Sans", sans-serif;
}

body {
  min-height: 100vh;
}

.app-shell {
  height: 100vh; /* lock page to viewport; sidebar lists scroll internally, page doesn't */
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  align-items: center;
  padding: 1.85rem 1.6rem 1.35rem 1.25rem; /* left aligns with sidebar; extra right margin for logos */
}

/* title block spans the sidebar column width (23rem) for left/right alignment */
.header-titles {
  width: 23rem;
  text-align: center; /* center the title on the sidebar's midline */
}

.eyebrow {
  margin: 0 0 0.35rem;
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.app-header h1 {
  margin: 0;
  font-size: clamp(2.2rem, 2.8vw, 3.2rem); /* larger title to fill the sidebar width */
  line-height: 1.05;
}

.subtitle {
  margin: 0.45rem 0 0;
  color: var(--muted);
  max-width: 42rem;
}

.header-note {
  display: flex;
  gap: 1.1rem;
  align-items: center;
  justify-content: flex-end;
}

.header-logos {
  display: inline-flex;
  align-items: center;
  gap: 1.1rem;
}

.header-logo-link {
  display: inline-flex;
  align-items: center;
}

.header-logos > .header-logo-link + .header-logo-link {
  border-left: 1px solid var(--border);
  padding-left: 1.1rem;
}

.header-logo {
  height: 3rem;
  width: auto;
  display: block;
  object-fit: contain;
  filter: drop-shadow(0 1px 2px rgba(28, 44, 54, 0.18));
}

.app-body {
  flex: 1;
  display: grid;
  grid-template-columns: 23rem minmax(0, 1fr);
  gap: 1rem;
  padding: 0 1.25rem 1.25rem;
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-height: 0;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 1.2rem;
  box-shadow: var(--shadow);
  padding: 1rem;
  backdrop-filter: blur(12px);
}

.panel-grow {
  flex: 1;
  min-height: 0; /* allow shrink so inner lists scroll instead of growing the page */
  display: flex;
  flex-direction: column;
}

.panel-heading h2 {
  margin: 0;
  font-size: 1.05rem;
}

.panel-heading p {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.search-form,
.field {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.search-form {
  gap: 0.8rem;
}

/* Focus panel children live directly in .panel (no wrapping form), so give them
   the same vertical rhythm the Search panel gets from .search-form */
.panel-focus {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.field span {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
}

.field select,
.field input {
  width: 100%;
  border: 1px solid rgba(21, 50, 61, 0.2);
  border-radius: 0.9rem;
  padding: 0.8rem 0.9rem;
  font: inherit;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.84);
}

.field select:focus,
.field input:focus {
  outline: 2px solid rgba(0, 187, 212, 0.24);
  border-color: rgba(0, 187, 212, 0.4);
}

.button-row {
  display: flex;
  gap: 0.65rem;
}

.button {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.72rem 1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, color 120ms ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button-primary {
  background: var(--button-dark);
  color: #fff;
}

.button-primary:hover {
  background: var(--button-dark-hover);
}

.button-secondary {
  background: var(--button-soft);
  color: var(--ink);
}

.result-list,
.pathway-list {
  margin-top: 0.85rem;
  flex: 1; /* take remaining panel height */
  min-height: 0; /* enable internal scroll instead of growing the panel */
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.empty-state {
  margin: 0;
  padding: 1rem;
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.66);
  color: var(--muted);
  border: 1px dashed rgba(21, 50, 61, 0.18);
}

.result-card,
.pathway-card {
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(21, 50, 61, 0.11);
  border-radius: 1rem;
  padding: 0.85rem;
  position: relative; /* anchor for copy-link icon */
  padding-right: 2rem;
}

/* deep-link copy icon (top-right of cards/popups) */
.copy-link-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--muted);
  opacity: 0.6;
  padding: 0.2rem;
}

.copy-link-btn:hover {
  opacity: 1;
}

.popup-card {
  position: relative;
}

.result-card.is-active,
.pathway-card.is-active {
  border-color: rgba(0, 187, 212, 0.44);
  box-shadow: inset 0 0 0 1px rgba(0, 187, 212, 0.22);
}

.result-head,
.pathway-head {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: flex-start;
}

.result-index {
  width: 1.9rem;
  height: 1.9rem;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  color: #fff;
  font-weight: 800;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.result-index.chemical {
  background: var(--chemical);
}

.result-index.reaction {
  background: var(--reaction);
}

.result-title,
.pathway-title {
  margin: 0;
  font-size: 1rem;
  line-height: 1.25;
}

.result-meta,
.pathway-meta {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.badge-row {
  margin-top: 0.55rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border-radius: 999px;
  padding: 0.24rem 0.55rem;
  font-size: 0.78rem;
  background: rgba(21, 50, 61, 0.08);
  color: var(--ink);
}

.result-actions,
.pathway-actions {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  margin-top: 0.8rem;
}

.inline-button {
  appearance: none;
  border: 1px solid rgba(21, 50, 61, 0.14);
  border-radius: 999px;
  padding: 0.45rem 0.72rem;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
}

.inline-button:hover {
  background: rgba(21, 50, 61, 0.05);
}

/* anchor styled as an inline-button (per-pathway Reference link) */
.inline-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.pathway-steps {
  margin: 0.85rem 0 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.pathway-step {
  appearance: none;
  width: 100%;
  border: 0;
  text-align: left;
  cursor: pointer;
  display: grid;
  grid-template-columns: 1.85rem 1fr;
  gap: 0.55rem;
  align-items: start;
  padding: 0.45rem 0.5rem;
  border-radius: 0.8rem;
  background: rgba(255, 255, 255, 0.72);
}

.pathway-step:hover {
  background: rgba(236, 244, 247, 0.94);
}

.pathway-step-index {
  width: 1.85rem;
  height: 1.85rem;
  border-radius: 999px;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
}

.pathway-step-index.chemical {
  background: var(--pathway-chemical);
}

.pathway-step-index.reaction {
  background: var(--pathway-reaction);
  color: #183329;
}

.step-title {
  margin: 0;
  font-size: 0.9rem;
}

.step-meta {
  margin: 0.18rem 0 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.map-shell {
  position: relative;
  min-height: 0;
  background: rgba(255, 255, 255, 0.26);
  border-radius: 1.4rem;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.55);
  box-shadow: var(--shadow);
}

.map-toolbar {
  position: absolute;
  top: 0.9rem;
  left: 0.9rem;
  z-index: 1000;
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
  max-width: calc(100% - 1.8rem);
}

.map-toolbar-card {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.65rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.84);
  border: 1px solid rgba(21, 50, 61, 0.12);
  font-size: 0.8rem;
}

.legend-swatch {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 999px;
  display: inline-block;
}

.legend-swatch.chemical {
  background: var(--chemical);
}

.legend-swatch.reaction {
  background: var(--reaction);
}

.legend-swatch.pathway-chemical {
  background: var(--pathway-chemical);
}

.legend-swatch.pathway-reaction {
  background: var(--pathway-reaction);
}

#map {
  width: 100%;
  height: 100%;
  min-height: calc(100vh - 12rem);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.4), rgba(214, 230, 235, 0.56)),
    #edf2f5;
}

.leaflet-container {
  font-family: "Trebuchet MS", "Gill Sans", sans-serif;
}

.chemmap-marker {
  width: 2rem;
  height: 2rem;
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 3px solid #fff;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 800;
  box-shadow: 0 10px 18px rgba(18, 38, 48, 0.28);
}

.chemmap-marker.chemical {
  background: var(--chemical);
}

.chemmap-marker.reaction {
  background: var(--reaction);
}

.chemmap-marker.pathway-chemical {
  background: var(--pathway-chemical);
}

.chemmap-marker.pathway-reaction {
  background: var(--pathway-reaction);
  color: #183329;
}

.leaflet-popup-content-wrapper {
  border-radius: 1rem;
}

.leaflet-popup-content {
  margin: 1rem;
  min-width: 17rem;
}

.popup-type {
  margin: 0 0 0.4rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.7rem;
  color: var(--muted);
}

.popup-title {
  margin: 0;
  font-size: 1rem;
  line-height: 1.25;
}

.popup-label,
.popup-description {
  margin: 0.45rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.popup-ref-list {
  margin: 0.75rem 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.popup-ref-list li {
  font-size: 0.84rem;
  line-height: 1.3;
}

.popup-ref-list strong {
  color: var(--ink);
}

.popup-ref-list a {
  color: #1d6a84;
  text-decoration: none;
}

.popup-ref-list a:hover {
  text-decoration: underline;
}

.popup-actions {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  margin-top: 0.85rem;
}

.app-footer {
  padding: 0 1.5rem 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
}

/* KAIST/MBEL logos as a bottom footer — hidden on desktop (the header carries them), shown only on mobile */
.footer-logos {
  display: none;
}

/* header status HUD — glassmorphism pill + live dot */
.status-hud {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
  max-width: 32rem;
  padding: 0.55rem 1.15rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.55);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  box-shadow: 0 6px 18px rgba(28, 44, 54, 0.1);
}

.status-dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--muted);
  flex-shrink: 0;
  transition: background 0.35s ease;
}

.status-text {
  font-size: 1rem;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-hud.is-active .status-dot {
  background: var(--reaction);
  animation: statusPulse 0.7s ease-out;
}

.status-hud.is-active .status-text {
  animation: statusFade 0.3s ease;
}

/* when there's no message, show only the dot, centered */
.status-hud.is-empty {
  justify-content: center;
}

.status-hud.is-empty .status-text {
  display: none;
}

@keyframes statusPulse {
  from { box-shadow: 0 0 0 0 rgba(0, 187, 212, 0.45); }
  to { box-shadow: 0 0 0 0.6rem rgba(0, 187, 212, 0); }
}

@keyframes statusFade {
  from { opacity: 0; transform: translateY(2px); }
  to { opacity: 1; transform: none; }
}

/* let the sidebar shrink and wrap long IDs (SMILES/InChI), summaries, and labels
   so narrow / mobile widths never force horizontal scrolling */
.sidebar {
  min-width: 0;
}

.badge,
.result-title,
.result-meta,
.pathway-meta,
.step-title,
.step-meta,
.popup-ref-list li {
  overflow-wrap: anywhere;
}

@media (max-width: 1120px) {
  /* stacked layout scrolls the page normally instead of locking to viewport */
  .app-shell {
    height: auto;
  }

  .header-titles {
    width: auto; /* full width on stacked/mobile layout */
  }

  .app-body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    order: 2;
  }

  .map-shell {
    order: 1;
    min-height: 34rem;
  }

  #map {
    min-height: 34rem;
  }
}

@media (max-width: 720px) {
  .app-header {
    flex-direction: column;
  }

  /* mobile header: center the title block and stack copy-link over the logo pair */
  .header-titles {
    text-align: center;
  }

  /* mobile: logos leave the header and move to the page-bottom footer (.footer-logos) */
  .header-note {
    display: none;
  }

  .footer-logos {
    display: flex;
    justify-content: center;
    padding-top: 1rem;
    margin-top: 0.5rem;
    border-top: 1px solid var(--border);
  }

  .app-body {
    padding: 0 0.85rem 0.85rem;
  }

  .app-header,
  .app-footer {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .button-row {
    flex-direction: column;
  }

  /* cap map height to the viewport (vs the 34rem tablet rule) and shrink the
     legend so it covers less of the map */
  .map-shell,
  #map {
    min-height: 62vh;
  }

  .map-toolbar-card {
    font-size: 0.7rem;
    padding: 0.3rem 0.5rem;
  }

  /* mobile: keep the HUD within the screen, wrap long status, center text + dot */
  .status-hud {
    max-width: 100%;
    order: 2; /* status HUD sits below the logos on mobile */
  }

  .status-text {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    text-align: center;
  }
}
