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

:root {
  --primary-color: #1f4e79;
  --success-color: #2e7d32;
  --warning-color: #f57c00;
  --error-color: #c62828;
  --light-bg: #f5f5f5;
  --border-color: #ddd;
  --text-dark: #333;
  --text-light: #666;
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  color: var(--text-dark);
  min-height: 100vh;
  padding: 20px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

/* ========== 页头 ========== */
.header {
  position: relative;
  background: linear-gradient(135deg, var(--primary-color) 0%, #2c5aa0 100%);
  color: white;
  padding: 40px 20px;
  text-align: center;
}

/* 语言切换 */
.lang-switch {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  gap: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  padding: 3px;
}
.lang-switch button {
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.85);
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
}
.lang-switch button:hover {
  background: rgba(255, 255, 255, 0.15);
}
.lang-switch button.active {
  background: #fff;
  color: var(--primary-color);
  font-weight: 600;
}

.header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 700;
}

.subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
  font-weight: 300;
}

/* ========== 主内容 ========== */
.main-content {
  padding: 40px;
}

/* ========== 表单样式 ========== */
.form-section {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.fieldset {
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 25px;
  background: #fafafa;
  transition: all 0.3s ease;
}

.fieldset:hover {
  border-color: var(--primary-color);
  background: #f0f6ff;
}

.fieldset.optional {
  border-color: #ccc;
  background: #f9f9f9;
}

.legend {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--primary-color);
  margin-bottom: 15px;
  padding-bottom: 10px;
  display: block;
}

.instruction {
  font-size: 0.9rem;
  color: var(--text-light);
  margin-bottom: 15px;
  padding: 10px;
  background: white;
  border-left: 3px solid var(--primary-color);
  border-radius: 4px;
  padding-left: 12px;
}

/* ========== 化合物输入 ========== */
.compounds-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 15px;
}

.compound-input {
  display: grid;
  grid-template-columns: 1fr 1fr 50px;
  gap: 10px;
  align-items: center;
}

.compound-name,
.compound-cas {
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.95rem;
  transition: border-color 0.2s;
}

.compound-name:focus,
.compound-cas:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 4px rgba(31, 78, 121, 0.2);
}

.btn-remove {
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 8px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.2s;
  color: var(--error-color);
}

.btn-remove:hover {
  background: var(--error-color);
  color: white;
  border-color: var(--error-color);
}

/* ========== 按钮 ========== */
.btn-add {
  background: #e8f5e9;
  border: 2px dashed var(--success-color);
  color: var(--success-color);
  padding: 10px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  font-size: 0.95rem;
}

.btn-add:hover {
  background: var(--success-color);
  color: white;
  border-style: solid;
}

.btn-primary,
.btn-secondary {
  padding: 12px 24px;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary-color) 0%, #2c5aa0 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(31, 78, 121, 0.3);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: #f5f5f5;
  border: 1px solid var(--border-color);
  color: var(--text-dark);
}

.btn-secondary:hover {
  background: #eee;
}

.button-group {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 30px;
}

.quota-note {
  margin: 12px auto 0;
  max-width: 620px;
  color: var(--text-light);
  font-size: 0.88rem;
  line-height: 1.55;
  text-align: center;
}

/* ========== 生成前免责声明确认 ========== */
.disclaimer-dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: min(82vh, 760px);
  margin: auto;
  padding: 0;
  border: 1px solid #cbd5df;
  border-radius: 8px;
  background: #fff;
  color: var(--text-dark);
  box-shadow: 0 20px 56px rgba(15, 35, 55, 0.28);
  overflow: hidden;
}

.disclaimer-dialog[open] {
  display: flex;
  flex-direction: column;
}

.disclaimer-dialog::backdrop {
  background: rgba(15, 30, 45, 0.62);
}

.disclaimer-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: 22px 24px 18px;
  border-bottom: 1px solid #e2e7ec;
}

.disclaimer-eyebrow {
  margin-bottom: 5px;
  color: #9a6700;
  font-size: 0.82rem;
  font-weight: 700;
}

.disclaimer-modal-header h2 {
  color: var(--primary-color);
  font-size: 1.35rem;
  line-height: 1.35;
}

.icon-button {
  flex: 0 0 44px;
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: #fff;
  color: #4b5563;
  cursor: pointer;
  font-size: 1.6rem;
  line-height: 1;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
}

.icon-button:hover {
  background: #f3f6f8;
  border-color: #aeb9c4;
  color: #1f2933;
}

.icon-button:focus-visible,
.btn-download:focus-visible {
  outline: 3px solid rgba(31, 78, 121, 0.28);
  outline-offset: 2px;
}

.disclaimer-modal-body {
  min-height: 0;
  padding: 18px 24px;
  overflow-y: auto;
  overscroll-behavior: contain;
  color: #3f4a55;
  font-size: 0.94rem;
  line-height: 1.75;
}

.disclaimer-modal-body p {
  margin-bottom: 14px;
}

.disclaimer-modal-body p:last-child {
  margin-bottom: 0;
}

.disclaimer-modal-footer {
  padding: 16px 24px 20px;
  border-top: 1px solid #e2e7ec;
  background: #f8fafb;
}

.disclaimer-consent {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 16px;
  color: #263746;
  cursor: pointer;
  font-size: 0.94rem;
  font-weight: 600;
  line-height: 1.55;
}

.disclaimer-consent input {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  margin-top: 1px;
  accent-color: var(--primary-color);
}

.disclaimer-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.disclaimer-actions .btn-primary,
.disclaimer-actions .btn-secondary {
  min-height: 44px;
}

.disclaimer-consent input:focus-visible,
.disclaimer-actions button:focus-visible {
  outline: 3px solid rgba(31, 78, 121, 0.28);
  outline-offset: 2px;
}

/* ========== 表单组 ========== */
.form-group {
  display: flex;
  flex-direction: column;
  margin-bottom: 16px;
}

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

.flex-1 {
  flex: 1;
}

.form-group label {
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-dark);
  font-size: 0.95rem;
}

.form-group input,
.form-group select {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  box-sizing: border-box;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background-color: #fff;
  color: var(--text-dark);
  font-size: 0.95rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:hover,
.form-group select:hover {
  border-color: #9cabb9;
}

.form-group input:focus-visible,
.form-group select:focus-visible {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(31, 78, 121, 0.14);
}

.form-group small {
  font-size: 0.85rem;
  color: var(--text-light);
  margin-top: 4px;
}

.input-with-unit {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 240px;
  gap: 12px;
  align-items: stretch;
}

.input-with-unit > input,
.input-with-unit > select {
  margin: 0;
}

.input-with-unit > select {
  white-space: nowrap;
}

/* ========== 分离目标：多产品 / 指定杂质 ========== */
.target-products-container,
.specific-impurities-container {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 15px;
}

.target-product-card {
  background: white;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 16px;
}

.target-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid #edf0f3;
}

.target-card-title {
  color: var(--primary-color);
  font-weight: 700;
  font-size: 1rem;
}

.target-subsection {
  margin-top: 22px;
  padding-top: 20px;
  border-top: 1px solid var(--border-color);
}

.target-subsection h3 {
  color: var(--primary-color);
  font-size: 1rem;
  margin-bottom: 6px;
}

.target-subsection p {
  color: var(--text-light);
  font-size: 0.88rem;
  line-height: 1.6;
  margin-bottom: 12px;
}

.constraint-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(140px, 180px) max-content;
  gap: 12px;
  align-items: end;
  background: #fff;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
}

.constraint-row .form-group {
  margin-bottom: 0;
}

.btn-remove-inline {
  align-self: end;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #fff5f5;
  border: 1px solid #f0c7c7;
  border-radius: 6px;
  color: var(--error-color);
  cursor: pointer;
  min-width: 104px;
  min-height: 39px;
  padding: 8px 12px;
  line-height: 1.2;
  white-space: nowrap;
  transition: all 0.2s;
}

.btn-remove-inline:hover {
  background: var(--error-color);
  border-color: var(--error-color);
  color: white;
}

.btn-add.compact {
  padding: 8px 14px;
  font-size: 0.9rem;
}

/* ========== 组成比例 ========== */
.composition-container {
  background: white;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 15px;
}

.composition-item {
  display: grid;
  grid-template-columns: 1fr 100px;
  gap: 12px;
  margin-bottom: 12px;
  align-items: center;
}

.composition-item:last-child {
  margin-bottom: 0;
}

.composition-label {
  font-weight: 500;
  color: var(--text-dark);
}

.composition-input {
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.9rem;
}

.composition-input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.composition-status {
  display: flex;
  gap: 10px;
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid var(--border-color);
  font-weight: 600;
}

.status-value {
  color: var(--primary-color);
  font-size: 1.1rem;
}

.warning-text {
  color: var(--warning-color);
  font-weight: 600;
  margin-left: auto;
}

/* ========== 结果展示 ========== */
.result-section {
  margin-top: 40px;
  padding: 25px;
  background: #f0f6ff;
  border: 2px solid var(--primary-color);
  border-radius: 8px;
}

/* PDF 预览框 */
.report-frame {
  width: 100%;
  height: 78vh;
  min-height: 480px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid var(--primary-color);
}

.result-header h2 {
  color: var(--primary-color);
  font-size: 1.5rem;
  margin-right: auto;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  text-decoration: none;
  white-space: nowrap;
}

.btn-download:hover {
  color: white;
  text-decoration: none;
}

.error-close {
  color: var(--error-color);
}

.error-close:hover {
  background: var(--error-color);
  border-color: var(--error-color);
  color: white;
}

.analysis-result {
  background: white;
  padding: 20px;
  border-radius: 6px;
  line-height: 1.8;
  font-size: 0.95rem;
  color: var(--text-dark);
  white-space: pre-wrap;
  word-break: break-word;
}

/* ========== 加载状态 ========== */
.loading-section {
  margin-top: 40px;
  padding: 40px;
  text-align: center;
  background: #f0f6ff;
  border: 2px dashed var(--primary-color);
  border-radius: 8px;
}

.loading-section[data-state="queued"] {
  background: #fffaf0;
  border-color: #b7791f;
}

.loading-section[data-state="queued"] .spinner {
  border-top-color: #b7791f;
}

.spinner {
  border: 4px solid var(--light-bg);
  border-top: 4px solid var(--primary-color);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading-section p {
  color: var(--text-dark);
  margin: 10px 0;
}

.loading-detail {
  color: var(--text-light);
  font-size: 0.9rem;
}

/* ========== 错误提示 ========== */
.error-section {
  margin-top: 40px;
  padding: 20px;
  background: #ffebee;
  border: 2px solid var(--error-color);
  border-radius: 8px;
}

.error-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  color: var(--error-color);
}

.error-header h3 {
  font-size: 1.2rem;
}

#errorMessage {
  color: var(--error-color);
  margin: 0;
}

/* ========== 页脚 ========== */
.footer {
  background: #f5f5f5;
  padding: 25px 40px;
  text-align: center;
  border-top: 1px solid var(--border-color);
  color: var(--text-light);
  font-size: 0.9rem;
}

.footer p {
  margin: 8px 0;
}

.footer code {
  background: #f0f0f0;
  padding: 2px 6px;
  border-radius: 3px;
  font-family: "Courier New", monospace;
  color: var(--primary-color);
}

/* ========== 响应式设计 ========== */
@media (max-width: 768px) {
  body {
    padding: 0;
    background: #fff;
  }

  .container {
    border-radius: 0;
    box-shadow: none;
  }

  .header {
    padding: 54px 14px 22px;
  }

  .header h1 {
    font-size: 1.7rem;
    line-height: 1.2;
  }

  .subtitle {
    font-size: 0.95rem;
  }

  .lang-switch {
    top: 10px;
    right: 10px;
  }

  .main-content {
    padding: 12px;
  }

  .fieldset {
    padding: 14px 12px;
    border-width: 1px;
  }

  .legend {
    font-size: 1rem;
    margin-bottom: 10px;
    padding-bottom: 6px;
  }

  .instruction {
    font-size: 0.82rem;
    line-height: 1.5;
    padding: 8px 10px;
  }

  .compound-input {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .form-row {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .input-with-unit {
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .target-product-card,
  .constraint-row,
  .composition-container {
    padding: 12px;
  }

  .target-card-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .target-remove-btn {
    align-self: stretch;
  }

  .constraint-row {
    grid-template-columns: 1fr;
  }

  .btn-remove-inline {
    width: 100%;
  }

  .report-frame {
    height: 65vh;
    min-height: 360px;
  }

  .result-section,
  .loading-section,
  .error-section {
    margin-top: 20px;
    padding: 14px;
  }

  .result-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 44px;
    align-items: center;
    gap: 10px;
  }

  .result-header h2 {
    font-size: 1.2rem;
    margin-right: 0;
  }

  .result-header .btn-download {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    text-align: center;
  }

  .result-header .result-close {
    grid-column: 2;
    grid-row: 1;
  }

  .analysis-result {
    padding: 14px;
    font-size: 0.88rem;
    line-height: 1.65;
    overflow-x: auto;
  }

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

  .quota-note {
    margin-top: 10px;
    padding: 0 4px;
    font-size: 0.82rem;
    text-align: left;
  }

  .btn-primary,
  .btn-secondary {
    width: 100%;
  }

  .disclaimer-dialog {
    width: calc(100vw - 20px);
    max-height: calc(100dvh - 20px);
  }

  .disclaimer-modal-header {
    padding: 16px 16px 14px;
    gap: 12px;
  }

  .disclaimer-modal-header h2 {
    font-size: 1.12rem;
  }

  .disclaimer-modal-body {
    padding: 14px 16px;
    font-size: 0.88rem;
    line-height: 1.68;
  }

  .disclaimer-modal-body p {
    margin-bottom: 12px;
  }

  .disclaimer-modal-footer {
    padding: 14px 16px 16px;
  }

  .disclaimer-consent {
    font-size: 0.88rem;
  }

  .disclaimer-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.35fr);
  }
}

/* ========== 打印样式 ========== */
@media print {
  body {
    background: white;
  }

  .button-group,
  .btn-download,
  .icon-button,
  .footer {
    display: none;
  }
}
