/* ============ 全局:hidden 属性优先于任何 display 规则 ============ */
[hidden] { display: none !important; }

/* ============ 主题变量 ============ */
:root, :root[data-theme="light"] {
  --bg: #f2f2f7;
  --card: #ffffff;
  --card-2: #f7f7fa;
  --text: #1c1c1e;
  --text-2: #8e8e93;
  --text-3: #c7c7cc;
  --accent: #007aff;
  --accent-press: #0062cc;
  --danger: #ff3b30;
  --green: #34c759;
  --orange: #ff9500;
  --border: rgba(0,0,0,.06);
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --sheet-shadow: 0 -8px 40px rgba(0,0,0,.12);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
/* 暗夜模式:纯黑为主 */
:root[data-theme="dark"] {
  --bg: #000000;
  --card: #1c1c1e;
  --card-2: #2c2c2e;
  --text: #f2f2f7;
  --text-2: #8e8e93;
  --text-3: #48484a;
  --accent: #0a84ff;
  --accent-press: #409cff;
  --danger: #ff453a;
  --green: #32d74b;
  --orange: #ff9f0a;
  --border: rgba(255,255,255,.08);
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  --sheet-shadow: 0 -8px 40px rgba(0,0,0,.6);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(96px + var(--safe-bottom));
  overscroll-behavior-y: none;
}

/* ============ 顶部 ============ */
.app-header {
  padding: calc(24px + var(--safe-top)) 16px 12px;
  background: var(--bg);
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: saturate(180%) blur(20px);
}
.title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.title-row h1 { font-size: 30px; font-weight: 800; letter-spacing: -0.5px; }
.header-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: none; background: var(--card); color: var(--accent);
  font-size: 16px; cursor: pointer; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { transform: scale(.92); }

.stats-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat-card {
  background: var(--card); border-radius: var(--radius); padding: 10px 6px;
  text-align: center; box-shadow: var(--shadow); cursor: pointer;
  transition: box-shadow .15s, transform .1s;
}
.stat-card:active { transform: scale(.96); }
.stat-card.active { box-shadow: inset 0 0 0 2px var(--accent), var(--shadow); }
.stat-num { font-size: 17px; font-weight: 700; white-space: nowrap; }
.stat-label { font-size: 11px; color: var(--text-2); margin-top: 2px; }

/* ============ 材料统计视图(树形) ============ */
.tree-row {
  background: var(--card); border-radius: var(--radius); padding: 12px 14px;
  box-shadow: var(--shadow); margin-bottom: 8px;
  display: flex; align-items: center; gap: 8px;
}
.tree-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-count { font-size: 16px; font-weight: 800; color: var(--accent); flex-shrink: 0; min-width: 22px; text-align: right; }
.tree-sub { font-size: 12px; color: var(--text-2); flex-shrink: 0; white-space: nowrap; }
.chevron { font-size: 11px; color: var(--text-3); width: 14px; flex-shrink: 0; transition: transform .15s; }
.tree-cat { font-weight: 800; font-size: 16px; }
.tree-cat .tree-name { color: var(--text); }
.tree-series { font-weight: 600; font-size: 14px; margin-left: 16px; }
.tree-series .tree-name { color: var(--text); }
.tree-color { font-size: 13px; margin-left: 32px; flex-wrap: wrap; row-gap: 6px; }
.tree-children[hidden] { display: none; }
.tree-row[data-toggle] { cursor: pointer; }
.tree-row[data-toggle]:active { filter: brightness(.96); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: 1px; }
.dot-spool { background: var(--green); }
.dot-refill { background: var(--orange); }
.tree-sep { display: inline-block; width: 10px; }
/* 统计树:无盘/含盘 可点击芯片 + 步进器 */
.stat-chip {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--card-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 4px 10px; font-size: 12px; color: var(--text); cursor: pointer; font-family: inherit;
}
.stat-chip:active { background: var(--text-3); }
.stat-chip .dot { margin-right: 0; }
.tree-sub { display: flex; align-items: center; gap: 6px; }
.stepper {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--card-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 4px;
}
.stepper button {
  width: 24px; height: 24px; border-radius: 50%; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; line-height: 1; background: var(--card);
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.stepper button:active { transform: scale(.9); }
.stepper .minus { color: var(--danger); }
.stepper .plus { color: var(--accent); }
.stepper b { min-width: 20px; text-align: center; font-size: 13px; color: var(--text); }

/* ============ 工具栏 ============ */
.toolbar { padding: 8px 16px 4px; position: sticky; top: calc(112px + var(--safe-top)); z-index: 9; background: var(--bg); }
.search-box {
  display: flex; align-items: center; gap: 8px;
  background: var(--card-2); border-radius: 10px; padding: 8px 12px;
}
.search-icon { font-size: 14px; opacity: .6; }
.search-box input {
  flex: 1; border: none; outline: none; background: transparent;
  font-size: 15px; color: var(--text); min-width: 0;
}
.search-box input::placeholder { color: var(--text-2); }
.chip-row { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 2px; scrollbar-width: none; }
.chip-row::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; border: none; cursor: pointer;
  padding: 6px 14px; border-radius: 999px; font-size: 13px;
  background: var(--card-2); color: var(--text-2); transition: all .15s;
}
.chip.active { background: var(--accent); color: #fff; font-weight: 600; }

/* ============ 列表 ============ */
.list-area { padding: 8px 16px; display: flex; flex-direction: column; gap: 10px; }
.spool-card {
  background: var(--card); border-radius: var(--radius); padding: 14px;
  box-shadow: var(--shadow); cursor: pointer; transition: transform .1s;
  border-left: 4px solid var(--swatch, #888);
  position: relative; z-index: 1; touch-action: pan-y;
  transition: transform .25s cubic-bezier(.32,.72,0,1);
}
.spool-card:active { filter: brightness(.96); }

/* ============ 左滑操作 ============ */
.swipe-item { position: relative; border-radius: var(--radius); overflow: hidden; }
.swipe-actions {
  position: absolute; top: 0; right: 0; bottom: 0; z-index: 0;
  display: flex; width: var(--swipe-w, 180px);
}
.swipe-btn {
  flex: 1; border: none; color: #fff; font-size: 14px; font-weight: 600;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  padding: 0 4px;
}
.swipe-btn:active { filter: brightness(.85); }
.swipe-edit { background: var(--accent); }
.swipe-delete { background: var(--danger); }
.swipe-cancel { background: #8e8e93; }
.spool-card.swiped { transform: translateX(calc(-1 * var(--swipe-w, 180px))); }
.spool-top { display: flex; align-items: center; gap: 10px; }
.swatch {
  width: 40px; height: 40px; border-radius: 10px; flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12);
}
.spool-name { flex: 1; min-width: 0; }
.spool-brand { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spool-meta { font-size: 12px; color: var(--text-2); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ============ 空状态 ============ */
.empty-state { text-align: center; padding: 80px 20px; color: var(--text-2); }
.empty-icon { font-size: 52px; margin-bottom: 12px; }
.empty-sub { font-size: 13px; margin-top: 6px; color: var(--text-3); }

/* ============ FAB ============ */
.fab {
  position: fixed; right: 20px; bottom: calc(28px + var(--safe-bottom)); z-index: 20;
  width: 58px; height: 58px; border-radius: 50%; border: none; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 28px; font-weight: 400; line-height: 1;
  box-shadow: 0 6px 20px rgba(0,122,255,.45);
  display: flex; align-items: center; justify-content: center;
  transition: transform .15s;
}
.fab:active { transform: scale(.9); }

/* ============ 批量选择 ============ */
.select-btn {
  background: none; border: none; color: var(--accent);
  font-size: 15px; font-weight: 600; padding: 4px 8px; cursor: pointer;
}
.select-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--card); border-radius: 16px 16px 0 0;
  box-shadow: 0 -2px 14px rgba(0,0,0,.1);
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
}
.select-bar span { font-size: 15px; font-weight: 600; color: var(--text); }
.select-bar button {
  background: var(--danger); color: #fff; border: none; border-radius: 12px;
  padding: 10px 26px; font-size: 15px; font-weight: 700; cursor: pointer;
}
.select-bar button:disabled { opacity: .4; }
.select-bar .select-all {
  background: none; color: var(--accent); padding: 10px 12px; font-weight: 600;
}
.sel-circle {
  position: absolute; top: 10px; right: 10px; z-index: 3;
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid var(--text-3, #c7c7cc);
  background: rgba(255,255,255,.9);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; color: transparent; line-height: 1;
}
.spool-card.selected { box-shadow: 0 0 0 2px var(--accent); }
.spool-card.selected .sel-circle { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ============ 底部弹层 ============ */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.4);
  animation: fadeIn .2s ease;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--card); border-radius: 18px 18px 0 0;
  box-shadow: var(--sheet-shadow); max-height: 88vh; overflow-y: auto;
  animation: slideUp .28s cubic-bezier(.32,.72,0,1);
  padding-bottom: var(--safe-bottom);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.sheet-handle { width: 36px; height: 5px; border-radius: 3px; background: var(--text-3); margin: 10px auto 0; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px 6px; }
.sheet-header h2 { font-size: 18px; font-weight: 700; }
.sheet-close {
  width: 30px; height: 30px; border-radius: 50%; border: none; cursor: pointer;
  background: var(--card-2); color: var(--text-2); font-size: 13px;
}
.sheet-body { padding: 6px 18px 20px; display: flex; flex-direction: column; gap: 12px; }

/* ============ 表单 ============ */
.field { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
.field-row { display: flex; gap: 12px; }
.field-label { font-size: 13px; color: var(--text-2); font-weight: 500; }
.req { color: var(--danger); font-style: normal; }
.field input[type="text"], .field input[type="number"], .field input[type="date"], .field select {
  width: 100%; border: 1px solid var(--border); background: var(--card-2);
  border-radius: 10px; padding: 11px 12px; font-size: 16px; color: var(--text);
  outline: none; appearance: none; -webkit-appearance: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.seg { display: flex; background: var(--card-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg-btn {
  flex: 1; border: none; background: transparent; color: var(--text-2);
  padding: 8px 0; border-radius: 8px; font-size: 14px; cursor: pointer; transition: all .15s;
}
.seg-btn.active { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 4px rgba(0,0,0,.12); }

.sheet-actions { display: flex; gap: 10px; margin-top: 6px; }
.btn {
  flex: 1; border: none; border-radius: 12px; padding: 14px; font-size: 16px; font-weight: 600;
  cursor: pointer; transition: transform .1s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: var(--card-2); color: var(--danger); }
.btn-block { width: 100%; margin-top: 12px; }
.ghost { flex: 0 0 auto; padding: 14px 18px; }

/* ============ 操作弹层 ============ */
.sheet-action { padding: 6px 14px calc(14px + var(--safe-bottom)); }
.action-title { text-align: center; font-size: 13px; color: var(--text-2); padding: 10px 0 6px; font-weight: 600; }
.action-list { display: flex; flex-direction: column; gap: 8px; }
.action-btn {
  width: 100%; border: none; background: var(--card-2); color: var(--text);
  border-radius: 12px; padding: 15px; font-size: 16px; cursor: pointer; text-align: center;
}
.action-btn:active { background: var(--text-3); }
.action-btn.danger { color: var(--danger); }
.action-btn.cancel { margin-top: 8px; font-weight: 600; }

/* ============ Toast ============ */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(28,28,30,.92); color: #fff; padding: 10px 20px; border-radius: 999px;
  font-size: 14px; z-index: 100; animation: fadeIn .2s ease; box-shadow: var(--sheet-shadow);
  max-width: 85vw; text-align: center;
}

/* 桌面端加宽约束 */
@media (min-width: 560px) {
  body { max-width: 560px; margin: 0 auto; box-shadow: 0 0 60px rgba(0,0,0,.08); min-height: 100vh; }
  .fab { right: calc(50% - 280px + 20px); }
}
