/* ============================================================
   PLC 常用算法生成器 — 样式（浅色 IDE 风格，代码区深色）
   ============================================================ */
:root {
  --primary: #0b6bcb;
  --primary-dark: #0958a8;
  --accent: #e8a13c;
  --bg: #f2f4f7;
  --panel-bg: #ffffff;
  --border: #d9dee5;
  --text: #1f2733;
  --text-dim: #6b7684;
  --code-bg: #1e2530;
  --code-fg: #d8dee9;
  --ok: #1a9e5c;
  --warn: #d9822b;
  --err: #c4382d;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------------- 顶部工具栏 ---------------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: linear-gradient(90deg, #0f2a4a, #123a63);
  color: #fff;
  box-shadow: var(--shadow);
  z-index: 10;
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .logo { font-size: 22px; }
.brand .title { font-size: 17px; font-weight: 700; letter-spacing: .5px; white-space: nowrap; }
.brand .subtitle { font-size: 11px; color: #9fb6cf; white-space: nowrap; }
.toolbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; }
#searchBox {
  width: 220px;
  padding: 6px 10px;
  border: 1px solid #2c4a6d;
  border-radius: 6px;
  background: #12304f;
  color: #fff;
  outline: none;
  font-size: 13px;
}
#searchBox::placeholder { color: #8ba4bd; }
.plat-label { font-size: 12px; color: #9fb6cf; }
#platformSelect {
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid #2c4a6d;
  background: #12304f;
  color: #fff;
  font-size: 13px;
  outline: none;
  cursor: pointer;
  max-width: 180px;
}
.btn {
  padding: 7px 14px;
  border: 1px solid #2c4a6d;
  border-radius: 6px;
  background: #17406b;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}
.btn:hover { background: #1e4f83; }
.btn-primary { background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-small { padding: 4px 10px; font-size: 12px; border-color: var(--border); background: #eef2f6; color: var(--text); }
.btn-small:hover { background: #e0e7ee; }

/* ---------------- 三栏布局 ---------------- */
.layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr) minmax(380px, 46%);
  gap: 10px;
  padding: 10px;
  min-height: 0;
}
.panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.panel-title {
  padding: 10px 14px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
  flex: 0 0 auto;
  font-size: 13px;
  color: var(--text-dim);
}

/* ---------------- 左侧分类树 ---------------- */
.tree { min-width: 0; }
#treeBody { overflow-y: auto; flex: 1; padding: 6px; }
.cat-node { margin-bottom: 2px; }
.cat-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  color: #2b3a4d;
}
.cat-head:hover { background: #eef3f9; }
.cat-head .arrow { width: 14px; font-size: 10px; color: var(--text-dim); transition: transform .12s; }
.cat-head.open .arrow { transform: rotate(90deg); }
.cat-head .cat-badge {
  background: var(--primary);
  color: #fff;
  font-size: 10px;
  border-radius: 4px;
  padding: 1px 5px;
  font-weight: 700;
}
.cat-head .cat-name { flex: 1; }
.cat-head .cat-count { font-size: 11px; color: var(--text-dim); }
.cat-body { display: none; }
.cat-body.open { display: block; }
.algo-leaf {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px 6px 26px;
  border-radius: 6px;
  cursor: pointer;
  color: #35455a;
  font-size: 13px;
  border-left: 3px solid transparent;
}
.algo-leaf:hover { background: #f0f5fb; }
.algo-leaf.active { background: #e3effb; border-left-color: var(--primary); color: var(--primary-dark); font-weight: 600; }
.algo-leaf .leaf-icon { font-size: 12px; opacity: .7; }

/* ---------------- 中间详情 ---------------- */
/* 让 HTML hidden 属性真正生效（避免 .detail-empty / #detailBody 的 display:flex 覆盖它） */
[hidden] { display: none !important; }
.detail { min-width: 0; }
.detail-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 8px;
}
.empty-icon { font-size: 46px; opacity: .35; }
#detailBody { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.detail-head { position: relative; padding: 14px 18px 10px; border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.detail-head h2 { margin: 0 0 6px; font-size: 19px; color: #123a63; }
.d-brief { margin: 0 0 8px; color: var(--text-dim); line-height: 1.6; }
.d-tags { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-fav {
  position: absolute;
  top: 12px;
  right: 14px;
  border: none;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: #b9c3cf;
  transition: color .15s, transform .15s;
  padding: 4px;
}
.btn-fav:hover { transform: scale(1.15); color: var(--accent); }
.btn-fav.on { color: #e8a13c; }
.btn-md-fav { top: 44px; font-size: 17px; }
.btn-md-fav:hover { color: var(--primary); }
.tag {
  font-size: 11px;
  background: #eef3f9;
  color: #35507a;
  border: 1px solid #d4e2f2;
  border-radius: 20px;
  padding: 2px 9px;
}
.tabs {
  display: flex;
  gap: 2px;
  padding: 0 10px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
  flex: 0 0 auto;
  overflow-x: auto;
}
.tab {
  border: none;
  background: transparent;
  padding: 10px 13px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.tab-content {
  flex: 1;
  overflow-y: auto;
  padding: 14px 18px 20px;
  line-height: 1.7;
}
.tab-content h4 { margin: 14px 0 6px; color: #123a63; font-size: 14px; }
.tab-content h4:first-child { margin-top: 0; }
.para { margin: 0 0 10px; }
.formula-box {
  background: #f7f9fc;
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  padding: 12px 14px;
  font-family: "Cambria Math", "Times New Roman", serif;
  font-size: 15px;
  white-space: pre-wrap;
  color: #24344a;
}
table.io-table { width: 100%; border-collapse: collapse; margin: 6px 0 12px; font-size: 13px; }
table.io-table th, table.io-table td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
table.io-table th { background: #f0f5fb; color: #35507a; font-weight: 600; }
table.io-table tr:nth-child(even) td { background: #fafbfd; }
.kind-io { font-size: 11px; border-radius: 4px; padding: 1px 6px; margin-right: 6px; }
.kind-input { background: #e7f3ec; color: #1a7a4c; }
.kind-output { background: #e8effa; color: #2756a6; }
.kind-internal { background: #fdf3e0; color: #9a6212; }
.kind-param { background: #f0e9fb; color: #6a3fb5; }
ul.c-list { margin: 4px 0 10px; padding-left: 20px; }
ul.c-list li { margin-bottom: 5px; }
.ref-list { list-style: none; padding: 0; margin: 0; }
.ref-list li { margin-bottom: 8px; padding-left: 18px; position: relative; word-break: break-all; }
.ref-list li::before { content: "🔗"; position: absolute; left: 0; top: 1px; font-size: 11px; }
.ref-list a { color: var(--primary); text-decoration: none; }
.ref-list a:hover { text-decoration: underline; }
.usage-pre {
  background: #f7f9fc;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 12.5px;
  overflow-x: auto;
  color: #24344a;
}

/* ---------------- 右侧代码面板 ---------------- */
.code-panel { min-width: 0; }
.code-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
  flex: 0 0 auto;
}
.file-name { font-family: "Cascadia Code", Consolas, monospace; font-size: 12.5px; color: #35507a; font-weight: 600; }
.spacer { flex: 1; }
.params-form {
  flex: 0 0 auto;
  max-height: 38%;
  overflow-y: auto;
  border-bottom: 1px solid var(--border);
  background: #fbfcfe;
  padding: 8px 12px 12px;
}
.params-title { font-size: 12px; font-weight: 700; color: var(--text-dim); margin: 2px 0 8px; }
.params-title small { font-weight: 400; color: #8b96a3; }
.param-row {
  display: grid;
  grid-template-columns: 130px minmax(70px, 110px) 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 7px;
}
.param-row label { font-size: 12.5px; color: #3a4a5e; font-weight: 600; text-align: right; }
.param-row label .unit { color: #8b96a3; font-weight: 400; }
.param-row input[type="number"], .param-row input[type="text"], .param-row select {
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 12.5px;
  font-family: "Cascadia Code", Consolas, monospace;
  outline: none;
  background: #fff;
  width: 100%;
}
.param-row input:focus, .param-row select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(11,107,203,.15); }
.param-desc { font-size: 11px; color: #7c8794; line-height: 1.4; }
.no-params { font-size: 12px; color: #8b96a3; padding: 4px 0 2px; }
.code-pre {
  flex: 1;
  margin: 0;
  overflow: auto;
  background: var(--code-bg);
  color: var(--code-fg);
  font-family: "Cascadia Code", "JetBrains Mono", Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  padding: 14px 16px;
  white-space: pre;
  tab-size: 4;
}
.code-pre code { font-family: inherit; }
/* 语法高亮 */
.hl-kw { color: #7fc1ff; font-weight: 600; }
.hl-cm { color: #6b7d94; font-style: italic; }
.hl-num { color: #f0a35e; }
.hl-str { color: #9ecb7a; }
.hl-type { color: #c5a5ff; }
.hl-time { color: #f7d478; }
.hl-op { color: #7fc1ff; }

/* ---------------- 页脚 ---------------- */
.footer {
  flex: 0 0 auto;
  padding: 6px 16px;
  font-size: 11px;
  color: var(--text-dim);
  background: #e9edf2;
  border-top: 1px solid var(--border);
}

/* 备案信息（右对齐） */
.footer-beian {
  float: right;
}
.footer-beian a {
  color: inherit;
  text-decoration: none;
}
.footer-beian a:hover {
  text-decoration: underline;
}

/* ---------------- 提示条 ---------------- */
.toast {
  position: fixed;
  top: 62px;
  right: 18px;
  background: #1f2937;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
  z-index: 99;
  opacity: 0;
  transform: translateY(-6px);
  transition: all .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.ok { background: #147a4d; }
.toast.err { background: #b3372c; }

/* 窄屏适配 */
@media (max-width: 1200px) {
  .layout { grid-template-columns: 200px minmax(0, 1fr) minmax(320px, 44%); }
  .brand .subtitle { display: none; }
}

/* ============================================================
   深色主题（data-theme="dark"，由顶部按钮切换，存 localStorage）
   ============================================================ */
[data-theme="dark"] {
  --bg: #131a24;
  --panel-bg: #1b2431;
  --border: #2b3746;
  --text: #d5dde8;
  --text-dim: #8fa0b4;
  --code-bg: #0e131b;
  --code-fg: #d8dee9;
  --primary: #4c9af0;
  --primary-dark: #3a83d4;
  --accent: #e8a13c;
  --shadow: 0 1px 4px rgba(0, 0, 0, .45);
}
[data-theme="dark"] .toolbar { background: linear-gradient(90deg, #0b1522, #10233d); }
[data-theme="dark"] #searchBox, [data-theme="dark"] #platformSelect {
  background: #122a47; border-color: #2c4a6d; color: #e4ecf5;
}
[data-theme="dark"] .btn { background: #15314f; border-color: #2c4a6d; color: #dbe6f2; }
[data-theme="dark"] .btn:hover { background: #1c3d63; }
[data-theme="dark"] .panel-title { background: #202b3a; color: #93a5bb; }
[data-theme="dark"] .cat-head { color: #c3cfde; }
[data-theme="dark"] .cat-head:hover { background: #243144; }
[data-theme="dark"] .algo-leaf { color: #b8c5d6; }
[data-theme="dark"] .algo-leaf:hover { background: #243144; }
[data-theme="dark"] .algo-leaf.active { background: #1e3c61; color: #8cc1f5; }
[data-theme="dark"] .detail-head h2 { color: #7db4ee; }
[data-theme="dark"] .tag { background: #22334a; color: #a9c6e8; border-color: #2f4a6e; }
[data-theme="dark"] .tabs { background: #1c2634; }
[data-theme="dark"] .tab { color: #8fa0b4; }
[data-theme="dark"] .tab.active { color: #7db4ee; }
[data-theme="dark"] .tab-content h4 { color: #7db4ee; }
[data-theme="dark"] .formula-box {
  background: #202b3a; border-color: #2b3746; color: #c9d6e6;
}
[data-theme="dark"] table.io-table th { background: #22334a; color: #a9c6e8; }
[data-theme="dark"] table.io-table th, [data-theme="dark"] table.io-table td { border-color: #2b3746; }
[data-theme="dark"] table.io-table tr:nth-child(even) td { background: #1d2836; }
[data-theme="dark"] .usage-pre { background: #202b3a; border-color: #2b3746; color: #c9d6e6; }
[data-theme="dark"] .code-toolbar { background: #1c2634; }
[data-theme="dark"] .file-name { color: #a9c6e8; }
[data-theme="dark"] .params-form { background: #192230; }
[data-theme="dark"] .param-row label { color: #b8c5d6; }
[data-theme="dark"] .param-row input[type="number"], [data-theme="dark"] .param-row input[type="text"],
[data-theme="dark"] .param-row select { background: #121a26; border-color: #2b3746; color: #d5dde8; }
[data-theme="dark"] .footer { background: #161e2a; color: #7e8fa4; }
[data-theme="dark"] .btn-small { background: #243144; border-color: #2b3746; color: #c3cfde; }
[data-theme="dark"] .btn-small:hover { background: #2d3c52; }
[data-theme="dark"] .btn-fav { color: #55657a; }
[data-theme="dark"] .btn-fav:hover { color: var(--accent); }
[data-theme="dark"] .cat-badge { background: #2c5c9c; }
[data-theme="dark"] .ref-list a { color: #6cb2f5; }
[data-theme="dark"] .kind-input { background: #1d3a2c; color: #7fd4a3; }
[data-theme="dark"] .kind-output { background: #243650; color: #8cb8f0; }
[data-theme="dark"] .kind-internal { background: #3d3320; color: #e0b25f; }
[data-theme="dark"] .kind-param { background: #352a4d; color: #b99ef0; }
