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

:root {
  --green: #3aad5e;
  --green-dark: #2e8b4c;
  --bg: #f2f4f3;
  --bar-bg: #ffffff;
  --border: #e0e4e2;
  --text: #2b2f2d;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
input, textarea {
  -webkit-user-select: text;
  user-select: text;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
}

/* 顶部栏 */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--border);
}

.app-title { font-weight: 700; font-size: 16px; }

.topbar-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.topbar-actions .btn { padding: 6px 10px; font-size: 12px; }

/* AI 分析遮罩：阻断一切操作 */
.busy-mask {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(15, 18, 17, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  touch-action: none;
}
.busy-mask[hidden] { display: none; }
.busy-card {
  width: min(320px, 100%);
  padding: 28px 22px;
  border-radius: 16px;
  background: #fff;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.busy-spinner {
  width: 36px;
  height: 36px;
  margin: 0 auto 14px;
  border: 3px solid #e0e4e2;
  border-top-color: var(--green);
  border-radius: 50%;
  animation: busy-spin .8s linear infinite;
}
@keyframes busy-spin { to { transform: rotate(360deg); } }
.busy-text {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.busy-sub {
  margin-top: 8px;
  font-size: 13px;
  color: #8a938f;
}
body.ai-busy {
  overflow: hidden;
  touch-action: none;
}
body.ai-busy .topbar,
body.ai-busy .bottombar,
body.ai-busy .modal-mask {
  pointer-events: none;
}


.btn {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { opacity: .75; }
.btn-primary {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

/* 画布区 */
.canvas-wrap {
  flex: 1 1 auto;
  position: relative;
  overflow: hidden;
  background: #e8ebe9;
}
#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}
.zoom-hint {
  position: absolute;
  right: 10px;
  top: 8px;
  font-size: 12px;
  color: #7a8480;
  background: rgba(255,255,255,.75);
  padding: 2px 8px;
  border-radius: 10px;
  pointer-events: none;
}

/* 底部栏 */
.bottombar {
  flex: 0 0 auto;
  background: var(--bar-bg);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
}
.toolbar-sep {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 2px;
  flex: 0 0 auto;
}
.tool-btn {
  flex: 0 0 auto;
  min-width: 44px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
}
.tool-btn.active {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}
.tool-btn:disabled { opacity: .35; cursor: default; }

/* 调色板 */
.palette-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.current-color {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 2px solid var(--green);
  background: #d32232;
}
.palette-strip {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(3, auto);
  gap: 4px;
  overflow-x: auto;
  padding: 3px 2px;
  -webkit-overflow-scrolling: touch;
}
.swatch {
  flex: 0 0 auto;
  width: 30px;
  height: 24px;
  border-radius: 5px;
  border: 1px solid rgba(0,0,0,.15);
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  line-height: 1;
}
.swatch.active {
  outline: 2px solid var(--green);
  outline-offset: 1px;
}

/* 弹窗 */
.modal-mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-mask[hidden] { display: none; }
.modal {
  background: #fff;
  border-radius: 14px;
  padding: 14px 14px 0;
  width: 100%;
  max-width: 400px;
  max-height: 80vh;
  max-height: 80dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.modal-title { font-size: 16px; font-weight: 700; }
.modal-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}
.modal-row label { flex: 0 0 auto; }
.modal-row input[type="range"] { flex: 1 1 auto; }
.modal-row input[type="number"] {
  flex: 1 1 auto;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.checkbox-label { display: flex; align-items: center; gap: 6px; }
.modal-info { font-size: 12px; color: #8a938f; }
/* 操作按钮吸底，内容再长也始终可见 */
.modal-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: #fff;
  padding: 10px 0 14px;
  margin-top: -2px;
  border-top: 1px solid #f0f2f1;
}
.modal-actions .btn { padding: 9px 18px; }

/* 尺寸档位按钮 */
.size-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.size-btn {
  flex: 1 1 auto;
  min-width: 56px;
  padding: 7px 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  cursor: pointer;
  text-align: center;
  color: var(--text);
  white-space: nowrap;
}
.size-btn.active {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  font-weight: 700;
}
.native-size-notice {
  padding: 8px 10px;
  border: 1px solid #b8dfc4;
  border-radius: 8px;
  background: #effaf2;
  color: var(--green-dark);
  font-size: 13px;
  text-align: center;
}

/* 裁剪区 */
#cropWrap { display: flex; flex-direction: column; gap: 6px; }
.crop-full-actions { display: none; }
.crop-canvas-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f2f1;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
#cropCanvas {
  max-width: 100%;
  touch-action: none;
  cursor: crosshair;
}
.crop-open-btn {
  align-self: flex-end;
  padding: 5px 12px;
}

/* 手机全屏裁剪器 */
#cropWrap.crop-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  padding: max(18px, env(safe-area-inset-top)) 14px
    max(22px, env(safe-area-inset-bottom));
  gap: 12px;
  background: #050505;
  overscroll-behavior: none;
  touch-action: none;
}
body.crop-active {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}
#cropWrap.crop-fullscreen .crop-full-actions {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 54px 1fr 54px;
  align-items: center;
}
.crop-full-title {
  color: #fff;
  font-size: 16px;
  text-align: center;
}
.crop-icon-btn {
  width: 46px;
  height: 46px;
  border: 0;
  border-radius: 50%;
  background: #fff;
  color: #000;
  font-size: 34px;
  line-height: 42px;
  cursor: pointer;
}
.crop-confirm-btn {
  justify-self: end;
  font-size: 30px;
}
#cropWrap.crop-fullscreen .crop-canvas-wrap {
  flex: 1 1 auto;
  min-height: 0;
  border: 0;
  background: #050505;
  overflow: visible;
}
#cropWrap.crop-fullscreen #cropCanvas {
  max-width: none;
  max-height: none;
}
#cropWrap.crop-fullscreen .crop-help {
  flex: 0 0 auto;
  padding: 6px 8px;
  color: #fff;
  font-size: 14px;
  text-align: center;
}
#cropWrap.crop-fullscreen .crop-open-btn { display: none; }

.crop-zoom-bar {
  display: none;
}
#cropWrap.crop-fullscreen .crop-zoom-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.crop-zoom-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: #fff;
  color: #111;
  font-size: 26px;
  line-height: 40px;
  cursor: pointer;
}
#cropZoomLabel {
  min-width: 52px;
  color: #fff;
  font-size: 14px;
  text-align: center;
}
.crop-zoom-reset {
  height: 34px;
  padding: 0 12px;
  border: 0;
  border-radius: 17px;
  background: #2a2a2a;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}

.btn-block {
  width: 100%;
  padding: 11px 12px;
  text-align: center;
}
.link-input {
  width: 100%;
  min-height: 96px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.45;
  resize: vertical;
  font-family: inherit;
  color: var(--text);
  background: #fff;
}
.link-input:focus {
  outline: none;
  border-color: var(--green);
}
.link-image-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.link-image-item {
  position: relative;
  aspect-ratio: 1;
  border: 2px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  padding: 0;
  background: #f0f2f1;
  cursor: pointer;
}
.link-image-item:active { opacity: .85; }
.link-image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #e8ebe9;
}
.link-image-item .link-image-idx {
  position: absolute;
  left: 6px;
  top: 6px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: rgba(0,0,0,.55);
  color: #fff;
  font-size: 12px;
  line-height: 22px;
  text-align: center;
}
.ai-key-row label { min-width: 58px; }
.ai-key-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}
#btnAiCrop {
  flex: 0 0 auto;
  padding: 6px 10px;
  white-space: nowrap;
}

.import-preview-wrap {
  background: repeating-conic-gradient(#e8e8e8 0% 25%, #fff 0% 50%) 0 0 / 16px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  max-height: 34vh;
  max-height: 34dvh;
  overflow: hidden;
}

/* 图纸模式：隐藏裁剪与尺寸，只展示优化结果 */
#importModal .modal.import-chart-only #cropWrap,
#importModal .modal.import-chart-only [data-photo-only] {
  display: none !important;
}
#importModal .modal.import-chart-only .import-preview-wrap {
  min-height: 240px;
  max-height: 55vh;
  max-height: 55dvh;
  order: -1;
}
#importModal .modal.import-chart-only #importPreview {
  max-height: 55vh;
  max-height: 55dvh;
}
#importModal .modal.import-chart-only {
  gap: 12px;
}
#importModal .modal.import-chart-only #importAiRow {
  display: flex !important;
  justify-content: flex-end;
}

#importPreview {
  image-rendering: pixelated;
  width: 100%;
  max-height: 34vh;
  max-height: 34dvh;
  object-fit: contain;
}

/* 统计 */
.stats-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 55vh;
  overflow-y: auto;
}
.stats-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  padding: 4px 2px;
  border-bottom: 1px solid #f0f2f1;
}
.stats-item .swatch { cursor: default; }
.stats-item .stats-name { flex: 1 1 auto; }
.stats-item .stats-count { font-weight: 700; }
.stats-total { font-size: 13px; color: #8a938f; padding-top: 4px; }
