:root {
  --bg: #f6f1e7;
  --bg-deep: #efe7d8;
  --card: #fffdf8;
  --ink: #221d18;
  --ink-soft: #4b433b;
  --muted: #8a7d6d;
  --line: #e6dccb;
  --line-strong: #d8cbb5;
  --accent: #e4572e;
  --accent-dark: #c6431e;
  --gold: #f2b705;
  --teal: #1f8a70;
  --shadow: 0 2px 0 rgba(34, 29, 24, 0.04), 0 10px 26px -18px rgba(34, 29, 24, 0.4);
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC",
    "Microsoft YaHei", "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overscroll-behavior: none;
}

#app {
  max-width: 780px;
  margin: 0 auto;
  min-height: 100%;
  position: relative;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------- 屏幕切换 ---------- */
.screen { display: none; flex-direction: column; min-height: 100vh; min-height: 100dvh; }
.screen.is-active { display: flex; }

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(14px + var(--safe-t)) 18px 14px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(var(--bg) 76%, rgba(246, 241, 231, 0));
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 800; letter-spacing: 0.02em; }
.brand span { font-size: 17px; }
.ghost-btn {
  margin-left: auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: var(--card);
  font-size: 16px;
  font-weight: 700;
  color: var(--ink-soft);
  display: grid;
  place-items: center;
  box-shadow: var(--shadow);
}
.editor-top { gap: 10px; padding-bottom: 8px; }
.editor-top .ghost-btn { margin-left: 0; }

/* ---------- 首页 ---------- */
.home-main { padding: 0 18px 40px; display: flex; flex-direction: column; gap: 16px; }
.hero h1 { font-size: 27px; line-height: 1.2; margin: 2px 0 8px; letter-spacing: -0.01em; }
.hero p { margin: 0; color: var(--muted); font-size: 13.5px; max-width: 30em; }

.dropzone {
  display: block;
  border: 2px dashed var(--line-strong);
  border-radius: 20px;
  background:
    radial-gradient(circle at 18px 18px, rgba(228, 87, 46, 0.07) 1.6px, transparent 1.7px) 0 0 / 18px 18px,
    var(--card);
  padding: 22px;
  transition: border-color 0.2s, background-color 0.2s;
  cursor: pointer;
}
.dropzone.is-drag { border-color: var(--accent); background-color: #fff4ee; }
.dz-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
  color: var(--ink-soft);
  padding: 12px 0;
}
.dz-inner svg { color: var(--accent); margin-bottom: 4px; }
.dz-inner strong { font-size: 16px; }
.dz-inner span { font-size: 12.5px; color: var(--muted); }
.dz-preview { position: relative; display: flex; flex-direction: column; gap: 8px; }
.dz-preview img {
  width: 100%;
  max-height: 240px;
  object-fit: contain;
  border-radius: 12px;
  background:
    linear-gradient(45deg, #f0eadf 25%, transparent 25%) 0 0 / 16px 16px,
    linear-gradient(-45deg, #f0eadf 25%, transparent 25%) 0 0 / 16px 16px,
    #faf6ee;
}
.dz-file { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dz-change {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(34, 29, 24, 0.72);
  color: #fff;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  backdrop-filter: blur(4px);
}

/* ---------- 卡片 / 设置 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.settings-card { padding: 6px 16px; }
.setting { padding: 14px 0 12px; border-bottom: 1px dashed var(--line); }
.setting:last-of-type { border-bottom: 0; }
.setting-head { display: flex; justify-content: space-between; align-items: baseline; font-weight: 600; }
.setting-head b { color: var(--accent-dark); font-size: 14px; font-variant-numeric: tabular-nums; }
.setting-foot { font-size: 12px; color: var(--muted); margin-top: 2px; }
.setting-foot.warn { color: var(--accent-dark); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 26px;
  margin: 6px 0 0;
  background: transparent;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent) var(--fill, 50%), var(--line) var(--fill, 50%));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  margin-top: -7px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
input[type="range"]::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: #fff; border: 3px solid var(--accent); }

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 13px 0;
  border-bottom: 1px dashed var(--line);
}
.switch-row:last-child { border-bottom: 0; }
.switch-row.tight { padding: 10px 0; }
.switch-row span { font-weight: 600; font-size: 14px; display: block; }
.switch-row small { color: var(--muted); font-size: 12px; display: block; }
.switch { position: relative; flex: 0 0 auto; width: 48px; height: 28px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; }
.switch i {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #ded5c5;
  transition: background 0.18s;
  pointer-events: none;
}
.switch i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.18s;
}
.switch input:checked + i { background: var(--teal); }
.switch input:checked + i::after { transform: translateX(20px); }

/* ---------- 按钮 ---------- */
.primary-btn {
  height: 52px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.02em;
  box-shadow: 0 8px 18px -10px rgba(228, 87, 46, 0.9);
  display: grid;
  place-items: center;
  transition: transform 0.12s, background 0.2s, opacity 0.2s;
}
.primary-btn:active { transform: scale(0.985); }
.primary-btn:disabled { background: #d9cdbb; box-shadow: none; color: #fff; }
.primary-btn.small { height: 40px; padding: 0 16px; font-size: 14px; border-radius: 12px; margin-left: auto; }
.primary-btn.is-busy { pointer-events: none; opacity: 0.8; }
.link-btn { color: var(--accent-dark); font-size: 13px; font-weight: 700; padding: 4px 0; }

.recent {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
}
.recent[hidden] { display: none; }
.recent-thumb {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: #ddd3c2;
  flex: 0 0 auto;
  background-size: cover;
  background-position: center;
}
.recent-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.recent-text b { font-size: 14px; }
.recent-text small { color: var(--muted); font-size: 12px; }
.recent-arrow { margin-left: auto; color: var(--muted); font-size: 22px; line-height: 1; }

.footnote { font-size: 11.5px; color: var(--muted); line-height: 1.6; margin: 4px 2px 0; }
.demo-line { font-size: 12.5px; color: var(--muted); text-align: center; margin: -6px 0 0; }
.ghost-line {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 12px;
  border-radius: 12px;
  border: 1px dashed var(--line-strong);
  color: var(--accent-dark);
  font-size: 13px;
  font-weight: 600;
}

/* ---------- 图纸页 ---------- */
#screen-editor { height: 100vh; height: 100dvh; min-height: 0; overflow: hidden; }
.editor-title { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.editor-title b { font-size: 15px; }
.editor-title span { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.editor-tools {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.editor-tools::-webkit-scrollbar { display: none; }
.tool {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 12px;
  border-radius: 11px;
  background: var(--card);
  border: 1px solid var(--line-strong);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  flex: 0 0 auto;
  transition: all 0.15s;
}
.tool svg { opacity: 0.85; }
.tool.is-on { background: var(--ink); border-color: var(--ink); color: #fff; }
.tool.icon-only { width: 36px; padding: 0; justify-content: center; font-size: 17px; }
.tool-spacer { flex: 1 1 8px; min-width: 2px; }
.tool-zoom { display: flex; align-items: center; gap: 7px; flex-shrink: 0; }

.stage {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  background:
    radial-gradient(circle at 12px 12px, rgba(34, 29, 24, 0.055) 1.2px, transparent 1.3px) 0 0 / 24px 24px,
    var(--bg-deep);
}
.canvas-scroll {
  position: absolute;
  inset: 0;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.canvas-scroll.is-panning { cursor: grabbing; }
#canvas {
  flex: 0 0 auto;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(34, 29, 24, 0.16), 0 18px 40px -28px rgba(34, 29, 24, 0.5);
  background: #fff;
  touch-action: none;
}
.stage-badge {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  background: rgba(34, 29, 24, 0.68);
  color: #fff;
  font-size: 11.5px;
  padding: 5px 12px;
  border-radius: 999px;
  pointer-events: none;
  backdrop-filter: blur(4px);
  transition: opacity 0.4s;
}

.dock {
  background: var(--card);
  border-top: 1px solid var(--line);
  padding: 10px 14px calc(10px + var(--safe-b));
  box-shadow: 0 -12px 30px -26px rgba(0, 0, 0, 0.5);
  flex: 0 0 auto;
}
.dock-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12.5px; color: var(--ink-soft); }
.dock-actions { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.dock-title b { color: var(--accent-dark); }
.dot-sep { color: var(--line-strong); margin: 0 4px; }
.progress { height: 4px; border-radius: 2px; background: #ece3d4; margin: 7px 0 9px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--teal); border-radius: 2px; transition: width 0.25s; }
.filter-hint {
  font-size: 12px;
  color: var(--ink-soft);
  background: #f3f8f5;
  border: 1px solid #d8ece3;
  border-radius: 9px;
  padding: 6px 9px;
  margin-bottom: 8px;
}
.filter-hint[hidden] { display: none; }

.swatches { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 4px; scrollbar-width: none; }
.swatches::-webkit-scrollbar { display: none; }
.swatch {
  flex: 0 0 auto;
  width: 66px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: #fff;
  padding: 6px 6px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  position: relative;
  transition: transform 0.12s, border-color 0.15s, box-shadow 0.15s;
}
.swatch:active { transform: scale(0.96); }
.swatch i {
  width: 100%;
  height: 30px;
  border-radius: 7px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  display: block;
}
.swatch b { font-size: 11.5px; letter-spacing: 0.01em; }
.swatch small { font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.swatch.is-on { border-color: var(--ink); box-shadow: 0 0 0 2px var(--ink); }
.swatch.is-done { opacity: 0.45; }
.swatch.is-done::after {
  content: "✓";
  position: absolute;
  top: 4px;
  right: 5px;
  font-size: 12px;
  color: #fff;
  background: var(--teal);
  width: 15px;
  height: 15px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  line-height: 1;
}

/* ---------- 底部面板 ---------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: flex-end;
  background: rgba(23, 19, 15, 0.42);
  animation: fade 0.18s ease;
}
.sheet[hidden] { display: none; }
.sheet-panel {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  background: var(--card);
  border-radius: 22px 22px 0 0;
  padding: 8px 20px calc(20px + var(--safe-b));
  max-height: 86vh;
  max-height: 86dvh;
  overflow-y: auto;
  animation: rise 0.22s cubic-bezier(0.2, 0.9, 0.3, 1);
  box-shadow: 0 -14px 40px -20px rgba(0, 0, 0, 0.5);
}
.sheet-grab { width: 38px; height: 4px; border-radius: 2px; background: var(--line-strong); margin: 4px auto 12px; }
.sheet h3 { margin: 0 0 12px; font-size: 17px; }
.sheet-foot { font-size: 12px; color: var(--muted); margin: 12px 0 0; }
.field-label { font-size: 12px; font-weight: 700; color: var(--muted); margin: 10px 0 6px; letter-spacing: 0.04em; }
.opt-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.opt-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 11px 6px;
  border-radius: 11px;
  border-bottom: 1px dashed var(--line);
  font-size: 14px;
}
.opt-item:last-child { border-bottom: 0; }
.opt-item .opt-demo {
  width: 40px;
  height: 40px;
  border-radius: 9px;
  background: #fff;
  border: 1px solid var(--line-strong);
  flex: 0 0 auto;
  position: relative;
  overflow: hidden;
  background-image:
    linear-gradient(to right, rgba(34, 29, 24, 0.16) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(34, 29, 24, 0.16) 1px, transparent 1px);
  background-size: 10px 10px;
}
.opt-item .opt-demo.d-dash { background-image: none; }
.opt-item .opt-demo.d-dot { background-image: radial-gradient(circle, rgba(34,29,24,.5) 1.1px, transparent 1.2px); background-size: 10px 10px; }
.opt-item .opt-demo.d-cross { background-image: none; }
.opt-item .opt-demo.d-none { background-image: none; background-color: #f6f1e7; }
.opt-item .opt-demo.d-cross::before,
.opt-item .opt-demo.d-cross::after {
  content: ""; position: absolute; inset: 0;
  background-image: linear-gradient(45deg, transparent 46%, rgba(34,29,24,.45) 46% 54%, transparent 54%);
}
.opt-item .opt-demo.d-cross::after { background-image: linear-gradient(-45deg, transparent 46%, rgba(34,29,24,.45) 46% 54%, transparent 54%); } 
.opt-item .opt-demo.d-dash::before {
  content: ""; position: absolute; inset: 0;
  background-image: repeating-linear-gradient(90deg, rgba(34,29,24,.35) 0 3px, transparent 3px 7px);
  background-size: 10px 100%;
}
.opt-item .opt-check { margin-left: auto; color: var(--teal); font-weight: 800; opacity: 0; }
.opt-item.is-on .opt-check { opacity: 1; }

.seg { display: flex; background: #f1e9db; border-radius: 11px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1;
  height: 38px;
  border-radius: 9px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink-soft);
}
.seg button.is-on { background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); color: var(--ink); }
.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 2px;
  border-bottom: 1px dashed var(--line);
  font-size: 14px;
}
.check-row input { width: 20px; height: 20px; accent-color: var(--teal); }
.sheet .primary-btn { margin-top: 16px; width: 100%; }

.help-list { padding-left: 20px; margin: 0; display: flex; flex-direction: column; gap: 10px; font-size: 14px; }
.help-list li { color: var(--ink-soft); }
.help-list b { color: var(--ink); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%);
  background: rgba(30, 25, 21, 0.94);
  color: #fff;
  font-size: 13px;
  border-radius: 12px;
  padding: 11px 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  z-index: 80;
  box-shadow: 0 12px 30px -12px rgba(0, 0, 0, 0.6);
  max-width: min(92vw, 460px);
  animation: toastIn 0.2s ease;
  pointer-events: none;
}
.toast[hidden] { display: none; }
.toast button { color: #ffd166; font-weight: 700; font-size: 13px; pointer-events: auto; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px); } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }

@media (min-width: 640px) {
  .hero h1 { font-size: 32px; }
  .home-main { padding-left: 24px; padding-right: 24px; }
}

/* 窄屏：工具栏改为两行，避免缩放按钮被裁掉 */
@media (max-width: 430px) {
  .editor-tools {
    flex-wrap: wrap;
    overflow-x: visible;
    row-gap: 8px;
  }
  .tool-spacer { display: none; }
  .tool-zoom { margin-left: auto; }
}

/* ---------- 导入现成图纸 / 裁剪 ---------- */
.import-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px 14px;
  margin-top: 14px;
  box-shadow: var(--shadow);
}
.import-icon {
  width: 40px;
  height: 40px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  color: var(--teal);
  background: rgba(31, 138, 112, 0.1);
}
.import-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.import-text b { font-size: 14.5px; }
.import-text small { color: var(--muted); font-size: 12px; line-height: 1.4; }

.crop-stage {
  position: relative;
  margin: 12px 0 10px;
  background: var(--bg-deep);
  border-radius: 14px;
  overflow: hidden;
  touch-action: none;
}
.crop-stage canvas { display: block; margin: 0 auto; touch-action: none; }
.crop-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.small-line { padding: 7px 12px; font-size: 13px; border-radius: 10px; }
#sheet-crop { z-index: 70; }
.small-seg { flex: 1 1 170px; }
.small-seg button { padding: 6px 8px; font-size: 12.5px; }

.pattern-preview {
  margin: 12px 0;
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-deep);
}
.pattern-preview img { display: block; width: 100%; max-height: 27vh; object-fit: contain; }
.pattern-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; margin: 14px 0 10px; }
.pattern-fields .small-line { grid-column: 1 / -1; }
.pattern-fields label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
  font-size: 12.5px;
  color: var(--muted);
}
.pattern-fields input {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
}
