* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: #1a1a1a; color: #e8e8e8; font-size: 14px;
  user-select: none;
}
.app { display: flex; flex-direction: column; height: 100vh; }

header {
  padding: 10px 16px; background: #222; border-bottom: 1px solid #3a3a3a;
  flex-shrink: 0;
}
.title-row { display: flex; align-items: baseline; gap: 16px; margin-bottom: 8px; flex-wrap: wrap; }
.title-row h1 { font-size: 16px; font-weight: 600; }
.hint { color: #888; font-size: 12px; }

.toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.toolbar .group { display: flex; gap: 2px; }
.toolbar button {
  background: #333; color: #e8e8e8; border: 1px solid #4a4a4a;
  padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 13px;
  transition: background 0.15s;
}
.toolbar button:hover:not(:disabled) { background: #444; }
.toolbar button.active { background: #0078d4; border-color: #0078d4; }
.toolbar button.primary { background: #107c10; border-color: #107c10; font-weight: 500; }
.toolbar button.primary:hover:not(:disabled) { background: #138a13; }
.toolbar button:disabled { background: #2a2a2a; color: #666; cursor: not-allowed; }
.toolbar .sep { width: 1px; height: 24px; background: #3a3a3a; margin: 0 6px; }

.brush-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 110px;
  height: 4px;
  background: #4a4a4a;
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  margin: 0 6px;
  vertical-align: middle;
}
.brush-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: #0078d4;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid #e8e8e8;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.brush-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: #0078d4;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid #e8e8e8;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.brush-value {
  color: #ccc;
  font-size: 11px;
  font-family: "Consolas", "Menlo", "Microsoft YaHei", monospace;
  min-width: 22px;
  text-align: right;
  margin: 0 2px 0 0;
}
.reset-images {
  font-size: 12px;
}

.status {
  margin-top: 6px; font-size: 12px; color: #888;
  font-family: "Consolas", "Menlo", "Microsoft YaHei", monospace;
}
.status.error { color: #f88; }
.status.success { color: #8f8; }

main { flex: 1; display: flex; overflow: hidden; min-height: 0; }

.stage-wrap { flex: 1; overflow: hidden; position: relative; background: #0a0a0a; }
.stage {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  overflow: auto;
  background-color: #0d0d0d;
  background-image:
    linear-gradient(45deg, #1a1a1a 25%, transparent 25%),
    linear-gradient(-45deg, #1a1a1a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1a1a1a 75%),
    linear-gradient(-45deg, transparent 75%, #1a1a1a 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  /* [M3-v3.1] iOS PWA 禁用 native 触摸手势 */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
.stage.mode-stroke { cursor: crosshair; }
.bg {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%; object-fit: contain;
  pointer-events: none; z-index: 1;
}
.ink-wrap {
  position: absolute; top: 0; left: 0;
  transform-origin: 0 0;
  z-index: 2;
  cursor: move;
  /* [M3-v3.1] 禁用 iOS 长按全选菜单 + 文本选择 */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  pointer-events: auto;  /* 必须接受 pointer 事件让 panZoom 工作 */
}
.stage.mode-stroke .ink-wrap { cursor: crosshair; }
.ink { display: block; pointer-events: none; -webkit-user-drag: none; }
#overlay {
  position: absolute; top: 0; left: 0;
  pointer-events: none; z-index: 3;
}
.brush-indicator {
  position: absolute; z-index: 4; pointer-events: none;
  border: 2px solid rgba(255, 200, 0, 0.7);
  border-radius: 50%; transform: translate(-50%, -50%);
  display: none;
  box-shadow: 0 0 8px rgba(255, 200, 0, 0.3);
}
.stage.mode-stroke .brush-indicator { display: block; }

.sidebar {
  width: 280px; background: #1a1a1a; border-left: 1px solid #3a3a3a;
  padding: 12px; overflow-y: auto; flex-shrink: 0;
}
.sidebar h3 { font-size: 14px; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.badge {
  background: #0078d4; color: white; font-size: 11px;
  padding: 1px 8px; border-radius: 10px; font-weight: 500;
}
.stroke-list { list-style: none; margin-bottom: 16px; }
.stroke-list li {
  padding: 8px 10px; background: #2a2a2a; margin-bottom: 4px;
  border-radius: 4px; font-size: 12px;
  display: flex; justify-content: space-between; align-items: center;
}
.stroke-list li.empty {
  color: #888; font-style: italic; line-height: 1.5;
  display: block; background: transparent; padding: 4px 0;
}
.stroke-list li > span { display: flex; align-items: center; }
.stroke-list .swatch {
  width: 14px; height: 14px; border-radius: 3px; margin-right: 8px;
  border: 1px solid rgba(255,255,255,0.2);
}
.stroke-list li button {
  background: #c42b1c; color: white; border: none;
  padding: 3px 8px; border-radius: 3px; cursor: pointer; font-size: 11px;
}
.stroke-list li button:hover { background: #d8392b; }

.stroke-list li .speed-wrap {
  display: inline-flex; align-items: center; gap: 4px;
  margin-right: 4px;
}
.stroke-list li .speed-label {
  color: #888; font-size: 10px;
}
.stroke-list li .speed-input {
  width: 44px; padding: 2px 4px;
  background: #1a1a1a; color: #e8e8e8;
  border: 1px solid #4a4a4a; border-radius: 3px;
  font-size: 11px; font-family: "Consolas", "Menlo", monospace;
  text-align: center;
}
.stroke-list li .speed-input:focus {
  outline: none; border-color: #0078d4;
}

/* M1.2 预览按钮(笔划列表) */
.stroke-list li .btn-preview {
  background: #0078d4; color: white; border: none;
  padding: 3px 8px; border-radius: 3px; cursor: pointer; font-size: 11px;
  margin-right: 4px;
}
.stroke-list li .btn-preview:hover { background: #1a8aff; }
.stroke-list li .btn-delete {
  background: #c42b1c; color: white; border: none;
  padding: 3px 8px; border-radius: 3px; cursor: pointer; font-size: 11px;
}
.stroke-list li .btn-delete:hover { background: #d8392b; }

/* M1.1 参数面板 */
.params-panel {
  background: #252525; border-radius: 4px; margin-bottom: 12px;
  padding: 8px 12px;
}
.params-panel > summary {
  font-size: 13px; font-weight: 500; color: #ccc; cursor: pointer;
  user-select: none; padding: 4px 0;
}
.params-panel > summary:hover { color: #fff; }
.params-advanced {
  margin-top: 8px; padding-left: 8px;
  border-left: 2px solid #3a3a3a;
}
.params-advanced > summary {
  font-size: 11px; color: #888; cursor: pointer; padding: 2px 0;
}
.param-group { margin: 8px 0; }
.param-group h4 {
  font-size: 11px; color: #888; font-weight: normal;
  margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px;
}
.param-row {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; margin-bottom: 4px; flex-wrap: wrap;
}
.param-row label {
  flex: 0 0 60px; color: #ccc;
}
.param-row input[type="range"] {
  flex: 1; min-width: 60px;
  height: 4px; background: #1a1a1a; outline: none; border-radius: 2px;
  -webkit-appearance: none; appearance: none;
}
.param-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 12px; height: 12px; background: #0078d4;
  border-radius: 50%; cursor: pointer;
}
.param-row input[type="range"]:focus {
  outline: 1px solid #0078d4;
}
.param-val {
  flex: 0 0 40px; text-align: right; font-family: "Consolas", "Menlo", monospace;
  color: #8f8; font-size: 11px;
}
.param-hint {
  flex: 0 0 100%; color: #777; font-size: 10px;
  margin-top: 1px; line-height: 1.3;
}

.tips { background: #252525; padding: 10px 12px; border-radius: 4px; }
.tips h4 { font-size: 12px; margin-bottom: 6px; color: #ccc; }
.tips ul { list-style: none; }
.tips li { font-size: 11px; color: #999; line-height: 1.6; padding: 2px 0; }
.tips li b { color: #ccc; font-weight: 500; }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7); z-index: 100;
  display: flex; align-items: center; justify-content: center;
}
.modal {
  background: #2a2a2a; padding: 24px 28px; border-radius: 8px;
  min-width: 360px; max-width: 600px; text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
}
.modal h3 { margin-bottom: 12px; }
.modal p { color: #aaa; font-size: 12px; margin: 4px 0; }
.modal .progress {
  height: 6px; background: #1a1a1a; border-radius: 3px;
  overflow: hidden; margin: 16px 0 8px;
}
.modal .progress-bar {
  height: 100%; background: #107c10; width: 0%; transition: width 0.15s;
}
.modal .actions { margin-top: 16px; display: flex; gap: 8px; justify-content: center; }
.modal .actions a, .modal .actions button {
  padding: 8px 16px; border-radius: 4px; text-decoration: none;
  font-size: 13px; cursor: pointer; border: none;
}
.modal .actions .dl { background: #107c10; color: white; }
.modal .actions .dl:hover { background: #138a13; }
.modal .actions .close { background: #444; color: white; }
.modal .actions .close:hover { background: #555; }
.modal video { margin-top: 16px; max-width: 100%; max-height: 50vh; border-radius: 4px; }

/* M1.2 单笔预览 modal */
.preview-modal-box {
  min-width: 480px; max-width: 90vw;
}
.preview-modal-box .preview-meta {
  font-size: 11px; color: #888; font-weight: 400; margin-left: 8px;
}
.preview-modal-box .preview-canvas {
  display: block; margin: 12px auto 0;
  max-width: 100%; max-height: 60vh;
  background: #000; border-radius: 4px;
}
.preview-modal-box .pt {
  color: #ccc; font-family: 'Consolas', 'Menlo', monospace; font-size: 11px;
  margin: 8px 0;
}
.preview-modal-box .actions { margin-top: 12px; }
.preview-modal-box .actions .replay {
  background: #0078d4; color: white;
  padding: 8px 16px; border-radius: 4px; border: none; cursor: pointer; font-size: 13px;
}
.preview-modal-box .actions .replay:hover { background: #1a8aff; }


/* M1.4 手动蒙版修正面板 */
.mask-panel {
  background: #252525; border-radius: 4px; margin-bottom: 12px;
  padding: 10px 12px; border: 1px solid #3a3a3a;
}
.mask-panel h3 { font-size: 13px; margin-bottom: 4px; color: #ccc; }
.mask-hint { font-size: 11px; color: #888; margin: 0 0 10px; line-height: 1.5; }
.mask-mode-row { display: flex; gap: 4px; margin-bottom: 8px; }
.mask-mode-btn {
  flex: 1; padding: 6px 8px; border-radius: 3px;
  background: #1a1a1a; color: #ccc; border: 1px solid #4a4a4a;
  font-size: 11px; cursor: pointer;
}
.mask-mode-btn.active { background: #0078d4; color: white; border-color: #0078d4; }
.mask-mode-btn[data-mode="erase"].active { background: #c42b1c; border-color: #c42b1c; }
.mask-mode-btn[data-mode="restore"].active { background: #107c10; border-color: #107c10; }
.mask-brush-row {
  display: flex; align-items: center; gap: 6px; margin-bottom: 8px;
  font-size: 11px; color: #888;
}
.mask-brush-row input[type=range] { flex: 1; }
.mask-brush-row span { color: #ccc; font-family: 'Consolas', 'Menlo', monospace; }
.mask-actions { display: flex; justify-content: space-between; align-items: center; }
.mask-action-btn {
  background: #444; color: #ccc; border: 1px solid #555;
  padding: 4px 10px; border-radius: 3px; font-size: 11px; cursor: pointer;
}
.mask-action-btn:hover { background: #555; color: white; }
.mask-inked-info { font-size: 11px; color: #888; font-family: 'Consolas', 'Menlo', monospace; }

/* M1.4 蒙版模式时 stage cursor 变十字,brush indicator 变彩色 */
.stage.mode-mask { cursor: crosshair; }
.brush-indicator.mode-mask-erase { border-color: rgba(255, 80, 80, 0.8) !important; background: rgba(255, 80, 80, 0.1) !important; }
.brush-indicator.mode-mask-restore { border-color: rgba(80, 255, 80, 0.8) !important; background: rgba(80, 255, 80, 0.1) !important; }

.preview-isolate-row {
  display: block; margin: 6px 0 0; font-size: 12px; color: #ccc; cursor: pointer;
  user-select: none;
}
.preview-isolate-row input { margin-right: 6px; vertical-align: middle; }

/* M2-B undo/redo 按钮 */
.undo-btn, .redo-btn {
  padding: 4px 10px; font-size: 12px;
  background: #2a2a2a; color: #ccc; border: 1px solid #4a4a4a;
  border-radius: 3px; cursor: pointer;
}
.undo-btn:hover:not(:disabled), .redo-btn:hover:not(:disabled) {
  background: #3a3a3a; color: white; border-color: #0078d4;
}
.undo-btn:disabled, .redo-btn:disabled {
  opacity: 0.4; cursor: not-allowed;
}

/* M2-F1 参考线面板 */
.guides-panel {
  background: #252525; border-radius: 4px; margin-bottom: 12px;
  padding: 10px 12px; border: 1px solid #3a3a3a;
}
.guides-panel h3 { font-size: 13px; margin-bottom: 4px; color: #ccc; }
.guides-info { font-size: 11px; color: #888; font-weight: 400; margin-left: 4px; }
.guides-hint { font-size: 11px; color: #888; margin: 0 0 10px; line-height: 1.5; }
.guides-hint b { color: #00d4ff; }
.guides-actions { display: flex; gap: 6px; }
.guides-btn {
  flex: 1; padding: 4px 8px; font-size: 11px;
  background: #2a2a2a; color: #ccc; border: 1px solid #4a4a4a;
  border-radius: 3px; cursor: pointer;
}
.guides-btn:hover { background: #3a3a3a; color: white; border-color: #0078d4; }

/* ===== M3 响应式:平板 (<=1023px) ===== */
@media (max-width: 1023px) {
  .sidebar { width: 240px; padding: 10px; }
  .toolbar { gap: 4px; }
  .toolbar button { padding: 5px 10px; font-size: 12px; }
  .brush-slider { width: 80px; }
  .toolbar .sep { margin: 0 4px; }
}

/* ===== M3 响应式:手机 (<=767px) ===== */
@media (max-width: 767px) {
  /* header 简化 */
  header { padding: 8px 10px; }
  .title-row { margin-bottom: 4px; gap: 8px; }
  .title-row h1 { font-size: 14px; }
  .hint { display: none; }
  .status { font-size: 11px; margin-top: 4px; }

  /* 抽屉 toggle 按钮(只在 mobile 显示) */
  .drawer-toggle {
    display: flex; align-items: center; justify-content: center;
    min-height: 44px; min-width: 44px;
    background: #0078d4; color: white;
    border: none; border-radius: 6px; padding: 6px 14px;
    font-size: 14px; font-weight: 500; cursor: pointer;
    margin-left: auto;
  }
  .drawer-toggle:active { background: #1a8aff; }

  /* main 竖排(stage 上,sidebar 下) */
  main { flex-direction: column; }

  /* 工具栏:横向滚动不换行,触摸友好 */
  .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }
  .toolbar .group { flex-shrink: 0; }
  .toolbar .sep { flex-shrink: 0; }

  /* 触摸目标 ≥ 44pt(Apple HIG) */
  .toolbar button {
    min-height: 44px; min-width: 44px;
    padding: 8px 12px; font-size: 14px;
  }
  .toolbar .undo-btn, .toolbar .redo-btn {
    min-height: 44px; padding: 8px 12px; font-size: 14px;
  }

  /* 滑块变粗(24px 高,拇指 28px) */
  .brush-slider {
    width: 120px; height: 24px;
  }
  .brush-slider::-webkit-slider-thumb {
    width: 28px; height: 28px;
  }
  .brush-slider::-moz-range-thumb {
    width: 28px; height: 28px;
  }
  .brush-value { font-size: 12px; min-width: 28px; }

  /* 抽屉(bottom sheet 风格) */
  .sidebar.drawer {
    position: fixed; bottom: 0; left: 0; right: 0;
    width: 100%; height: auto; max-height: 70vh;
    background: #1a1a1a;
    border-left: none; border-top: 1px solid #3a3a3a;
    border-radius: 16px 16px 0 0;
    padding: 12px 16px 16px;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.5);
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    z-index: 50;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .sidebar.drawer.open { transform: translateY(0); }

  /* 抽屉顶部拖把 */
  .sidebar.drawer::before {
    content: '';
    display: block;
    width: 40px; height: 4px;
    background: #555; border-radius: 2px;
    margin: -4px auto 12px;
  }

  /* 抽屉遮罩 */
  .drawer-backdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.55);
    opacity: 0; pointer-events: none;
    transition: opacity 0.28s ease;
    z-index: 49;
  }
  .drawer-backdrop.open { opacity: 1; pointer-events: auto; }

  /* 笔划列表项触摸优化 */
  .stroke-list li {
    padding: 10px 12px; font-size: 13px;
  }
  .stroke-list li button {
    min-height: 36px; padding: 6px 12px; font-size: 12px;
  }
  .stroke-list li .speed-input {
    min-height: 32px; font-size: 13px;
  }

  /* 参数面板 */
  .params-panel { padding: 10px; }
  .param-row {
    padding: 4px 0;
    min-height: 40px;
  }
  .param-row input[type="range"] {
    height: 24px;
  }
  .param-row input[type="range"]::-webkit-slider-thumb {
    width: 24px; height: 24px;
  }

  /* 蒙版 / 参考线 */
  .mask-panel, .guides-panel { padding: 10px; }
  .mask-mode-btn, .guides-btn {
    min-height: 44px; padding: 10px 12px; font-size: 13px;
  }
  .mask-brush-row input[type=range] { height: 24px; }

  /* modal 改 bottom sheet */
  .modal-backdrop { align-items: flex-end; }
  .modal {
    min-width: 0; width: 100%; max-width: 100%;
    border-radius: 16px 16px 0 0;
    padding: 20px 16px;
  }
  .preview-modal-box { width: 100%; }
}
