/* ===== 基础重置与变量 ===== */
:root {
  --bg-page: #FAFAF7;
  --bg-card: #FFFFFF;
  --text-primary: #1A1A1A;
  --text-secondary: #444444;
  --text-muted: #666666;
  --accent: #003B5C;
  --accent-light: #E6EDF1;
  --border: #E5E2D7;
  --border-strong: #CFC9B8;
  --shadow-sm: 0 1px 3px rgba(0, 59, 92, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 59, 92, 0.08);
  --radius-sm: 8px;
  --radius-md: 16px;
  --radius-pill: 9999px;
  --max-width: 1200px;
  --header-bg: rgba(250, 250, 247, 0.92);
  --hero-grad-top: #EAF1F6;
  --font-stack: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", "Noto Sans SC", "Inter", system-ui, sans-serif;
}

/* ===== 深色模式变量 ===== */
:root[data-theme="dark"] {
  --bg-page: #0F1419;
  --bg-card: #1A2128;
  --text-primary: #E8ECF1;
  --text-secondary: #B4BCC6;
  --text-muted: #8A93A0;
  --accent: #5AA9DB;
  --accent-light: #1E2A35;
  --border: #2A323B;
  --border-strong: #3A4651;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.5);
  --header-bg: rgba(15, 20, 25, 0.92);
  --hero-grad-top: #16222E;
}

*, *::before, *::after {
  box-sizing: border-box;
}

/* hidden 属性必须真正隐藏（否则会盖过弹窗/工具条的 display） */
[hidden] {
  display: none !important;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font-stack);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text-primary);
  background-color: var(--bg-page);
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.container {
  width: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 24px;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* ===== 顶部导航 ===== */
.header {
  position: sticky;
  top: 0;
  z-index: 50;
  background-color: var(--header-bg);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--accent);
  font-weight: 700;
  font-size: 20px;
  text-decoration: none;
}

.logo-icon {
  flex-shrink: 0;
}

.nav {
  display: flex;
  align-items: center;
  gap: 28px;
}

.nav-link {
  font-size: 15px;
  color: var(--text-secondary);
  transition: color 0.15s ease;
}

.nav-link:hover,
.nav-link.is-active {
  color: var(--text-primary);
  text-decoration: none;
}

/* ===== 深色模式切换按钮 ===== */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  background-color: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.theme-toggle:hover {
  color: var(--accent);
  border-color: var(--border-strong);
}

.theme-toggle .icon-sun {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

:root[data-theme="dark"] .theme-toggle .icon-sun {
  display: inline;
}

/* ===== Hero 区 ===== */
.hero {
  padding: 72px 0 56px;
  text-align: center;
  background: linear-gradient(180deg, var(--hero-grad-top) 0%, var(--bg-page) 100%);
  transition: background 0.2s ease;
}

.hero-title {
  margin: 0 0 16px;
  font-size: clamp(32px, 5vw, 44px);
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
}

.hero-subtitle {
  margin: 0 auto 32px;
  max-width: 640px;
  font-size: 18px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 20px;
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s ease, border-color 0.15s ease, background-color 0.2s ease;
}

.search-bar:focus-within {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
}

.search-icon {
  flex-shrink: 0;
  color: var(--text-muted);
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 16px;
  color: var(--text-primary);
  font-family: inherit;
}

.search-input::placeholder {
  color: #888888;
}

.result-count {
  margin-top: 12px;
  min-height: 24px;
  font-size: 14px;
  color: var(--text-secondary);
}

/* ===== 分类卡片 ===== */
.categories {
  padding: 32px 0 64px;
}

.section-title {
  margin: 0 0 28px;
  font-size: 28px;
  font-weight: 600;
  color: var(--text-primary);
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
}

.category-card {
  display: flex;
  flex-direction: column;
  padding: 24px;
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease, background-color 0.2s ease, border-color 0.2s ease;
}

.category-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.category-card.is-hidden {
  display: none;
}

.category-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin-bottom: 14px;
  border-radius: 12px;
  background-color: var(--accent-light);
  color: var(--accent);
}

.category-icon svg {
  width: 24px;
  height: 24px;
}

.category-title {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
}

.category-desc {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}

/* 分类头部（图标 + 标题 + 管理按钮） */
.category-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
}

.category-head-text {
  flex: 1;
  min-width: 0;
}

.category-links {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ===== 链接：悬浮动画 + 中文简介气泡 ===== */
.category-link {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 14px;
  color: var(--text-primary);
  background-color: transparent;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.category-link:hover {
  background: linear-gradient(90deg, var(--accent-light) 0%, rgba(0, 0, 0, 0) 100%);
  color: var(--accent);
  transform: translateX(4px);
  box-shadow: var(--shadow-sm);
  text-decoration: none;
}

.link-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 右侧工具组：外链箭头 + 编辑 + 删除（统一竖向居中对齐） */
.link-tools {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
}

.link-arrow {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 26px;
  color: var(--text-muted);
  font-size: 13px;
  transition: transform 0.2s ease, color 0.2s ease;
}

.category-link:hover .link-arrow {
  transform: translate(2px, -2px);
  color: var(--accent);
}

/* 悬浮简介气泡 */
.category-link::before {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  transform: translateX(-50%) translateY(6px) scale(0.96);
  width: max-content;
  max-width: 260px;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 12.5px;
  line-height: 1.55;
  text-align: left;
  white-space: normal;
  box-shadow: 0 8px 24px rgba(0, 59, 92, 0.28);
  opacity: 0;
  pointer-events: none;
  z-index: 30;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.category-link:hover::before,
.category-link:focus-visible::before {
  opacity: 1;
  transform: translateX(-50%) translateY(0) scale(1);
}

.category-link[data-tip=""]::before {
  display: none;
}

.category-link.is-hidden {
  display: none;
}

/* 卡片悬浮时图标微亮 */
.category-card:hover .category-icon {
  transform: scale(1.06);
  box-shadow: 0 4px 14px rgba(0, 59, 92, 0.15);
}

.category-icon {
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

/* ===== 管理面板（?edit=1） ===== */
.admin-bar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  background-color: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-md);
}

.admin-hint {
  font-size: 12.5px;
  color: var(--text-muted);
  max-width: 320px;
}

.btn {
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  padding: 7px 14px;
  font-size: 13.5px;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.btn-primary {
  background-color: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-ghost {
  background-color: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ===== 管理控件默认隐藏（访客视图干净）===== */
/* 分类编辑/删除按钮、链接编辑/删除按钮、添加链接按钮、底部工具条，默认都不显示 */
.cat-actions,
.mini-btn,
.add-link-btn,
.admin-bar {
  display: none;
}

/* 仅当网址带 ?edit=1 时 body 带 edit-mode，才显示全部管理控件 */
body.edit-mode .cat-actions {
  display: flex;
  gap: 6px;
}

body.edit-mode .mini-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

body.edit-mode .add-link-btn {
  display: inline-block;
}

body.edit-mode .admin-bar {
  display: flex;
}

.category-link {
  flex-wrap: wrap;
}

.mini-btn {
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  border-radius: 8px;
  width: 26px;
  height: 26px;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.4;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease, opacity 0.15s ease;
}

.category-link:hover .mini-btn,
.category-link:focus-within .mini-btn,
.cat-actions .mini-btn:hover {
  opacity: 1;
}

.mini-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.link-del:hover {
  color: #C0392B;
  border-color: #C0392B;
}

.add-link-btn {
  align-self: flex-start;
  margin-top: 4px;
  border: 1px dashed var(--border-strong);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.add-link-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ===== 编辑弹窗 ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background-color: rgba(15, 20, 25, 0.5);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.modal {
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  animation: modal-pop 0.18s ease;
}

@keyframes modal-pop {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-head h3 {
  margin: 0;
  font-size: 17px;
  color: var(--text-primary);
}

.modal-close {
  border: none;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
}

.modal-body {
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 13px;
  color: var(--text-secondary);
}

.field input,
.field textarea {
  width: 100%;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text-primary);
  background-color: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s ease;
}

.field input:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.field textarea {
  resize: vertical;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

/* ===== 关于 ===== */
.about {
  padding: 32px 0 72px;
  border-top: 1px solid var(--border);
  transition: border-color 0.2s ease;
}

.about p {
  max-width: 720px;
  margin: 0;
  color: var(--text-secondary);
  line-height: 1.7;
}

/* ===== 页脚 ===== */
.footer {
  padding: 24px 0;
  border-top: 1px solid var(--border);
  background-color: var(--bg-page);
  text-align: center;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.footer p {
  margin: 0;
  font-size: 14px;
  color: var(--text-secondary);
}

/* ===== 顶部功能 Tab ===== */
.tabs {
  display: flex;
  gap: 8px;
  padding-top: 20px;
  padding-bottom: 4px;
}

.tab {
  border: 1px solid var(--border);
  background-color: var(--bg-card);
  color: var(--text-secondary);
  border-radius: var(--radius-pill);
  padding: 8px 20px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.tab:hover {
  color: var(--accent);
  border-color: var(--border-strong);
}

.tab.is-active {
  background-color: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ===== 科研小工具 ===== */
.tools {
  padding: 24px 0 64px;
}

.tools-intro {
  margin: -12px 0 28px;
  color: var(--text-secondary);
}

.tool-card {
  padding: 24px;
  margin-bottom: 24px;
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

/* 图像色彩分析器：独立 iframe 模块。宽度撑满容器（解除 .tool-body 的 640px 限制）；
   高度由脚本按内容自动设置（postMessage），内部不再出现滚动条，整页滚动代替。 */
.tool-card--wide .tool-body {
  max-width: none;
}
.tool-iframe {
  display: block;
  width: 100%;
  height: 780px; /* 兜底高度，加载后由脚本按真实内容高度覆盖 */
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background-color: #f5f6f8;
}

@media (max-width: 720px) {
  .tool-card--wide .tool-body {
    max-width: none;
  }
}

.tool-title {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
}

.tool-desc {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.tool-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 640px;
}

.tool-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.tool-row .field {
  flex: 1 1 140px;
  min-width: 120px;
}

/* select 与 input 统一样式 */
.field select {
  width: 100%;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text-primary);
  background-color: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s ease;
}

.field select:focus {
  outline: none;
  border-color: var(--accent);
}

.tool-result {
  font-size: 15px;
  line-height: 1.7;
  color: var(--text-primary);
  background-color: var(--accent-light);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}

.tool-result:empty {
  display: none;
}

.tool-result.warn {
  background-color: rgba(192, 57, 43, 0.1);
  color: #C0392B;
}

.formula-note {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-muted);
}

/* 系数表折叠 */
.coeff-table {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 14px;
  font-size: 14px;
}

.coeff-table summary {
  cursor: pointer;
  color: var(--text-secondary);
  padding: 8px 0;
}

.coeff-table-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin-bottom: 10px;
}

.coeff-table-body th,
.coeff-table-body td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: left;
  color: var(--text-secondary);
}

.coeff-table-body th {
  background-color: var(--accent-light);
  color: var(--text-primary);
}

/* LD50 表 */
.ld-table-wrap {
  overflow-x: auto;
}

.ld-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.ld-table th,
.ld-table td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: center;
  color: var(--text-secondary);
}

.ld-table th {
  background-color: var(--accent-light);
  color: var(--text-primary);
}

.ld-table input {
  width: 90px;
  padding: 6px 8px;
  font-size: 14px;
  font-family: inherit;
  text-align: center;
  color: var(--text-primary);
  background-color: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.ld-table input:focus {
  outline: none;
  border-color: var(--accent);
}

/* 细胞计数 */
.cell-total {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  padding: 6px 0;
}

.cell-total.is-limit {
  color: #C0392B;
  animation: cell-flash 0.6s ease 3;
}

@keyframes cell-flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.cell-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cell-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background-color: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.cell-name {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  color: var(--text-primary);
}

.cell-key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  background-color: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.cell-count {
  min-width: 36px;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
}

.cell-btn {
  border: 1px solid var(--border);
  background-color: var(--bg-card);
  color: var(--text-secondary);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.cell-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.cell-actions {
  display: flex;
  gap: 10px;
}

/* 随机数 */
.tool-textarea {
  width: 100%;
  min-height: 120px;
  padding: 12px;
  font-size: 13px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  color: var(--text-primary);
  background-color: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  resize: vertical;
}

.tool-checks {
  gap: 20px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
}

/* ===== 响应式 ===== */
@media (max-width: 768px) {
  .hero {
    padding: 48px 0 40px;
  }

  .hero-subtitle {
    font-size: 16px;
  }

  .search-bar {
    padding: 10px 16px;
  }

  .nav {
    gap: 16px;
  }

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

  .tab {
    padding: 7px 16px;
    font-size: 14px;
  }

  .tool-row .field {
    flex-basis: 100%;
  }

  .cell-row {
    gap: 8px;
  }
}
