:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --surface-strong: #eef2f5;
  --line: #d8dee4;
  --line-strong: #b8c2cc;
  --text: #17212b;
  --muted: #61707f;
  --muted-2: #84919e;
  --accent: #1463a6;
  --accent-dark: #0f4d80;
  --accent-soft: #e5f0f9;
  --green: #167247;
  --green-soft: #e4f3eb;
  --amber: #9b5b00;
  --amber-soft: #fff2d8;
  --red: #b3342d;
  --red-soft: #fbe7e5;
  --shadow: 0 12px 28px rgb(19 35 48 / 8%);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--bg);
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

button,
input,
select {
  font: inherit;
}

button {
  color: inherit;
}

svg {
  display: block;
}

.app-header {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  min-height: 72px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 10px 22px;
  border-bottom: 1px solid var(--line);
  background: rgb(255 255 255 / 96%);
  backdrop-filter: blur(10px);
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 8px;
  color: white;
  background: linear-gradient(145deg, #1b76bd, #0e568f);
}

.brand-mark svg {
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 680;
  letter-spacing: 0;
}

.brand p {
  margin: 1px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.header-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.button,
.icon-text-button,
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line-strong);
  background: white;
  cursor: pointer;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    color 120ms ease,
    transform 120ms ease;
}

.button {
  min-height: 36px;
  gap: 7px;
  padding: 7px 12px;
  border-radius: 6px;
  white-space: nowrap;
}

.button:hover,
.icon-text-button:hover,
.icon-button:hover {
  border-color: #8fa1b1;
  background: #f7fafc;
}

.button:active,
.icon-text-button:active,
.icon-button:active {
  transform: translateY(1px);
}

.button svg,
.icon-text-button svg,
.icon-button svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.button-primary {
  border-color: var(--accent);
  color: white;
  background: var(--accent);
}

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

.button-quiet {
  color: #334758;
  border-color: #d5dde4;
  background: #f7f9fa;
}

.button-danger-quiet {
  color: var(--red);
  border-color: #e8bbb7;
  background: #fff8f7;
}

.icon-button {
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  border-radius: 6px;
}

.icon-text-button {
  min-height: 32px;
  gap: 6px;
  padding: 5px 9px;
  border-radius: 6px;
  color: #425566;
  font-size: 12px;
}

.app-shell {
  display: grid;
  min-height: calc(100vh - 72px);
  grid-template-columns: minmax(292px, 340px) minmax(0, 1fr);
}

.search-panel {
  display: flex;
  min-height: calc(100vh - 72px);
  flex-direction: column;
  padding: 18px 16px 16px;
  border-right: 1px solid var(--line);
  background: var(--surface);
}

.field-label,
.temperature-control label,
.profile-control label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.select,
.search-input,
.temperature-control input,
.temperature-control select,
.profile-control select {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  color: var(--text);
  outline: none;
  background: white;
}

.select {
  width: 100%;
  height: 36px;
  padding: 0 32px 0 10px;
}

.search-form {
  display: grid;
  margin-top: 12px;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.search-input-wrap {
  position: relative;
  min-width: 0;
}

.search-input-wrap > svg {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 17px;
  height: 17px;
  fill: none;
  stroke: var(--muted);
  stroke-linecap: round;
  stroke-width: 1.8;
}

.search-input {
  width: 100%;
  height: 38px;
  padding: 0 38px 0 34px;
}

.search-input:focus,
.select:focus,
.temperature-control input:focus,
.temperature-control select:focus,
.profile-control select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgb(20 99 166 / 12%);
}

.search-input-wrap .icon-button {
  position: absolute;
  top: 3px;
  right: 3px;
  border-color: transparent;
  background: transparent;
}

.search-summary {
  min-height: 20px;
  margin: 12px 2px 8px;
  color: var(--muted);
  font-size: 12px;
}

.result-list {
  display: flex;
  min-height: 160px;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 5px;
  overflow: auto;
  padding-right: 2px;
}

.result-item {
  display: grid;
  width: 100%;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  text-align: left;
  background: transparent;
  cursor: pointer;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px 10px;
}

.result-item:hover {
  border-color: #d7e2eb;
  background: #f4f8fb;
}

.result-item.is-active {
  border-color: #9fc0dc;
  background: var(--accent-soft);
}

.result-name {
  overflow: hidden;
  font-weight: 650;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-id {
  color: var(--muted-2);
  font-size: 11px;
}

.result-meta {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
  grid-column: 1 / -1;
}

.result-empty {
  padding: 24px 12px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}

.search-hint {
  display: flex;
  gap: 6px;
  margin-top: 12px;
  padding: 10px;
  border: 1px solid #dbe5ed;
  border-radius: 6px;
  color: var(--muted);
  background: #f7fafc;
  font-size: 12px;
}

.search-hint strong {
  color: #3d5264;
}

.detail-panel {
  min-width: 0;
  padding: 22px;
}

.empty-state {
  display: grid;
  min-height: calc(100vh - 120px);
  align-content: center;
  justify-items: center;
  text-align: center;
}

.empty-symbol {
  display: grid;
  width: 88px;
  height: 88px;
  place-items: center;
  border: 1px solid #cbd8e2;
  border-radius: 50%;
  color: #6b8ba4;
  background: #f7fafc;
}

.empty-symbol svg {
  width: 52px;
  height: 52px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-width: 1.7;
}

.empty-state h2 {
  margin: 18px 0 4px;
  font-size: 22px;
}

.empty-state p {
  max-width: 460px;
  margin: 0;
  color: var(--muted);
}

.database-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-top: 22px;
}

.database-stats div {
  min-width: 100px;
}

.database-stats strong {
  display: block;
  color: var(--accent-dark);
  font-size: 21px;
}

.database-stats span {
  color: var(--muted);
  font-size: 12px;
}

.substance-hero {
  display: grid;
  align-items: start;
  gap: 20px;
  grid-template-columns: 180px minmax(0, 1fr) auto;
}

.structure-frame {
  position: relative;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  box-shadow: var(--shadow);
}

.structure-frame img {
  display: block;
  width: 100%;
  height: 154px;
  object-fit: contain;
}

/* 原库未收录结构图时的占位提示（避免出现空白框） */
.structure-frame.is-missing::after {
  content: "原库未收录结构图";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 154px;
  border-radius: 6px;
  background: repeating-linear-gradient(45deg, #f7fafc, #f7fafc 8px, #eef3f7 8px, #eef3f7 16px);
  color: var(--muted-2);
  font-size: 12px;
}

/* hidden 属性必须生效（部分 display 规则会覆盖浏览器默认值） */
.empty-state[hidden],
.detail-content[hidden],
img[hidden] {
  display: none !important;
}

.structure-caption {
  margin-top: 7px;
  overflow: hidden;
  color: var(--muted-2);
  text-align: center;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.identity {
  min-width: 0;
  padding-top: 2px;
}

.identity-topline {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}

.identity-id,
.identity-family {
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
}

.identity-id {
  color: #31546d;
  background: #e8f1f7;
}

.identity-family {
  color: #486044;
  background: #edf4e8;
}

.identity h2 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 30px;
  line-height: 1.15;
  letter-spacing: 0;
}

.english-name {
  margin: 3px 0 13px;
  color: var(--muted);
  font-size: 14px;
}

.identity-grid {
  display: grid;
  max-width: 670px;
  grid-template-columns: repeat(4, minmax(110px, 1fr));
  gap: 8px;
}

.identity-grid div {
  min-width: 0;
  padding: 8px 9px;
  border-left: 3px solid #c5d8e7;
  background: #f7fafc;
}

.identity-grid span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.identity-grid strong {
  display: block;
  overflow: hidden;
  margin-top: 2px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.alias-block {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 12px;
}

.alias-label {
  flex: 0 0 auto;
  padding-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.alias-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.alias-chip {
  max-width: 260px;
  overflow: hidden;
  padding: 2px 7px;
  border: 1px solid #dce3e8;
  border-radius: 4px;
  color: #506271;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: #fafcfd;
  font-size: 11px;
}

.alias-more {
  border-color: #c7d8e6;
  color: var(--accent-dark);
  background: #eef5fa;
  cursor: pointer;
}

.hero-actions {
  display: flex;
  gap: 8px;
}

.tabs {
  display: flex;
  gap: 2px;
  margin-top: 24px;
  border-bottom: 1px solid var(--line);
}

.tab {
  position: relative;
  min-height: 39px;
  padding: 8px 14px;
  border: 0;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
}

.tab::after {
  position: absolute;
  right: 8px;
  bottom: -1px;
  left: 8px;
  height: 2px;
  background: transparent;
  content: "";
}

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

.tab.is-active {
  color: var(--accent-dark);
  font-weight: 650;
}

.tab.is-active::after {
  background: var(--accent);
}

.tab-panel {
  display: none;
  padding-top: 18px;
}

.tab-panel.is-active {
  display: block;
}

.calculation-toolbar {
  display: flex;
  align-items: end;
  gap: 12px;
  flex-wrap: wrap;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.temperature-control {
  display: grid;
  grid-template-columns: 112px 78px;
}

.temperature-control label {
  grid-column: 1 / -1;
}

.temperature-control input {
  width: 112px;
  height: 34px;
  padding: 0 9px;
  border-radius: 6px 0 0 6px;
}

.temperature-control select {
  width: 78px;
  height: 34px;
  padding: 0 6px;
  margin-left: -1px;
  border-radius: 0 6px 6px 0;
}

.profile-control select {
  min-width: 130px;
  height: 34px;
  padding: 0 8px;
}

.export-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.range-note {
  min-height: 20px;
  margin: 10px 2px 7px;
  color: var(--muted);
  font-size: 12px;
}

.range-note.is-warning {
  color: var(--amber);
}

.property-results {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.property-result-row {
  display: grid;
  align-items: center;
  padding: 11px 13px;
  border-bottom: 1px solid #e6ebef;
  grid-template-columns: minmax(160px, 1.2fr) minmax(132px, 1fr) minmax(118px, 0.8fr) minmax(100px, 0.7fr);
  gap: 12px;
}

.property-result-row:last-child {
  border-bottom: 0;
}

.property-result-row.is-unavailable {
  color: var(--muted-2);
  background: #fafbfc;
}

.property-result-name strong {
  display: block;
}

.property-result-name span,
.property-result-range,
.property-result-formula {
  color: var(--muted);
  font-size: 11px;
}

.property-value {
  font-variant-numeric: tabular-nums;
  font-size: 17px;
  font-weight: 650;
}

.property-value small {
  margin-left: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}

.status-pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
}

.status-ok {
  color: var(--green);
  background: var(--green-soft);
}

.status-warning {
  color: var(--amber);
  background: var(--amber-soft);
}

.status-missing {
  color: #66727c;
  background: #edf0f2;
}

.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 11px;
}

.section-heading h3 {
  margin: 0;
  font-size: 17px;
}

.section-heading span {
  color: var(--muted);
  font-size: 12px;
}

.property-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(138px, 1fr));
  gap: 8px;
}

.property-tile {
  min-height: 67px;
  padding: 10px 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
}

.property-tile span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.property-tile strong {
  display: block;
  overflow: hidden;
  margin-top: 4px;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.property-tile small {
  display: block;
  margin-top: 2px;
  color: var(--muted-2);
  font-size: 10px;
}

.table-scroll {
  max-height: 620px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.data-table th,
.data-table td {
  padding: 7px 9px;
  border-bottom: 1px solid #e7ecf0;
  text-align: left;
  vertical-align: top;
}

.data-table th {
  position: sticky;
  z-index: 2;
  top: 0;
  color: #445666;
  background: #f5f8fa;
}

.data-table td:first-child {
  width: 110px;
  color: var(--accent-dark);
  font-family: Consolas, monospace;
}

.data-table td:last-child {
  width: 210px;
  color: var(--muted);
}

.formula-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(240px, 1fr));
  gap: 12px;
}

.formula-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  overflow: hidden;
}

.formula-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 11px;
  border-bottom: 1px solid #e5eaee;
  background: #f8fafb;
}

.formula-card-head strong {
  font-size: 13px;
}

.formula-card-head span {
  color: var(--muted);
  font-size: 11px;
}

.formula-card img {
  display: block;
  width: 100%;
  height: 110px;
  object-fit: contain;
  padding: 9px;
  background: white;
}

.app-dialog {
  width: min(760px, calc(100vw - 28px));
  max-height: min(84vh, 760px);
  padding: 0;
  border: 1px solid #aebbc6;
  border-radius: 8px;
  color: var(--text);
  background: white;
  box-shadow: 0 24px 70px rgb(15 25 35 / 25%);
}

.app-dialog::backdrop {
  background: rgb(14 22 30 / 45%);
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  padding: 15px 17px;
  border-bottom: 1px solid var(--line);
  background: #f8fafb;
}

.dialog-head h2 {
  margin: 0;
  font-size: 18px;
}

.dialog-head p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.dialog-content {
  max-height: 66vh;
  overflow: auto;
  padding: 16px 17px;
}

.dialog-content h3 {
  margin: 14px 0 5px;
  font-size: 14px;
}

.dialog-content h3:first-child {
  margin-top: 0;
}

.dialog-content p {
  margin: 5px 0;
  color: #4e5e6d;
  white-space: pre-wrap;
}

.dialog-content code {
  padding: 1px 4px;
  border-radius: 3px;
  background: #eef2f5;
}

.history-groups {
  display: grid;
  max-height: 58vh;
  overflow: auto;
  padding: 14px 17px;
  gap: 12px;
}

.history-group {
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

.history-group h3 {
  margin: 0;
  padding: 7px 10px;
  border-bottom: 1px solid var(--line);
  background: #f6f8fa;
  font-size: 12px;
}

.history-items {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 9px 10px;
}

.history-chip {
  padding: 3px 8px;
  border: 1px solid #cdd9e2;
  border-radius: 4px;
  color: var(--accent-dark);
  background: #f4f9fc;
  cursor: pointer;
}

#clear-history {
  margin: 0 17px 16px;
}

.toast {
  position: fixed;
  z-index: 50;
  right: 18px;
  bottom: 18px;
  max-width: min(380px, calc(100vw - 36px));
  padding: 9px 12px;
  border-radius: 6px;
  color: white;
  background: #243746;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition:
    opacity 180ms ease,
    transform 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1180px) {
  .identity-grid,
  .property-grid {
    grid-template-columns: repeat(2, minmax(130px, 1fr));
  }

  .substance-hero {
    grid-template-columns: 160px minmax(0, 1fr);
  }

  .hero-actions {
    grid-column: 1 / -1;
  }
}

@media (max-width: 900px) {
  .app-header {
    position: static;
  }

  .brand p {
    display: none;
  }

  .app-shell {
    min-height: 0;
    grid-template-columns: 1fr;
  }

  .search-panel {
    min-height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .result-list {
    max-height: 260px;
  }

  .detail-panel {
    padding: 16px;
  }

  .property-result-row {
    grid-template-columns: minmax(150px, 1fr) minmax(120px, 1fr) auto;
  }

  .property-result-range {
    display: none;
  }

  .formula-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  .app-header {
    min-height: 62px;
    padding: 9px 12px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .brand h1 {
    font-size: 17px;
  }

  .header-actions .button span {
    display: none;
  }

  .header-actions .button {
    width: 36px;
    padding: 0;
  }

  .search-form {
    grid-template-columns: 1fr;
  }

  .search-form > .button-primary {
    display: none;
  }

  .substance-hero {
    grid-template-columns: 112px minmax(0, 1fr);
    gap: 12px;
  }

  .structure-frame img {
    height: 104px;
  }

  .identity h2 {
    font-size: 23px;
  }

  .english-name {
    margin-bottom: 8px;
  }

  .identity-grid {
    grid-template-columns: 1fr;
  }

  .identity-grid div {
    padding: 5px 7px;
  }

  .alias-block {
    grid-column: 1 / -1;
  }

  .tabs {
    overflow-x: auto;
    margin-top: 18px;
  }

  .tab {
    flex: 0 0 auto;
    padding-right: 11px;
    padding-left: 11px;
  }

  .calculation-toolbar {
    align-items: stretch;
  }

  .temperature-control,
  .profile-control {
    flex: 1 1 170px;
  }

  .temperature-control {
    grid-template-columns: minmax(88px, 1fr) 68px;
  }

  .temperature-control input {
    width: 100%;
  }

  .profile-control select {
    width: 100%;
  }

  .export-actions {
    width: 100%;
    margin-left: 0;
  }

  .export-actions button {
    flex: 1;
  }

  .property-result-row {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .property-result-formula {
    display: none;
  }

  .property-grid {
    grid-template-columns: 1fr 1fr;
  }

  .section-heading {
    align-items: flex-start;
    flex-direction: column;
    gap: 2px;
  }

  .data-table td:last-child {
    display: none;
  }
}

@media print {
  body {
    background: white;
  }

  .app-header,
  .search-panel,
  .tabs,
  .hero-actions,
  .calculation-toolbar,
  .export-actions,
  .dialog-head,
  .toast {
    display: none !important;
  }

  .app-shell {
    display: block;
  }

  .detail-panel {
    padding: 0;
  }

  .tab-panel {
    display: block !important;
    break-before: page;
  }

  .property-result-row,
  .property-tile {
    break-inside: avoid;
  }
}
