/* ===== CSS 变量 ===== */
:root {
  --bg-primary: #f8f9fa;
  --bg-panel: #ffffff;
  --bg-canvas: #e9ecef;
  --bg-hover: #f1f3f5;
  --bg-active: #e3f2fd;
  --border-color: #dee2e6;
  --border-light: #e9ecef;
  --text-primary: #212529;
  --text-secondary: #6c757d;
  --text-muted: #adb5bd;
  --accent: #4263eb;
  --accent-hover: #3b5bdb;
  --accent-light: #e7f5ff;
  --danger: #e03131;
  --danger-light: #fff5f5;
  --success: #2f9e44;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.15);
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --topbar-h: 56px;
  --left-w: 200px;
  --right-w: 280px;
  --status-h: 36px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

/* ===== Reset ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select { font-family: inherit; font-size: inherit; }
canvas { display: block; }

/* ===== 顶部导航栏 ===== */
.topbar {
  height: var(--topbar-h);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: relative;
  z-index: 100;
  box-shadow: var(--shadow-sm);
}
.topbar-left, .topbar-center, .topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.menu-btn { display: none; padding: 6px; border-radius: var(--radius-sm); color: var(--text-secondary); }
.menu-btn:hover { background: var(--bg-hover); }
.logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.logo-icon { font-size: 22px; }

.mode-switch {
  display: flex;
  background: var(--bg-primary);
  border-radius: var(--radius-md);
  padding: 3px;
  border: 1px solid var(--border-color);
}
.mode-btn {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all .2s;
  white-space: nowrap;
}
.mode-btn.active { background: var(--bg-panel); color: var(--accent); font-weight: 600; box-shadow: var(--shadow-sm); }

.size-control { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text-secondary); }
.size-control label { white-space: nowrap; }
.size-input {
  width: 52px;
  padding: 4px 8px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  text-align: center;
  font-size: 13px;
  background: var(--bg-panel);
}
.size-input:focus { outline: none; border-color: var(--accent); }
.size-x { color: var(--text-muted); }

/* ===== 按钮 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg-hover); }
.btn-ghost:disabled { opacity: .35; cursor: not-allowed; }
.btn-danger-ghost { color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-light); }
.btn-full { width: 100%; }

/* ===== 主区域布局 ===== */
.main-area {
  display: flex;
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
}

/* 左侧工具栏 */
.left-panel {
  width: var(--left-w);
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  padding: 12px;
  overflow-y: auto;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.panel-section { display: flex; flex-direction: column; gap: 8px; }
.panel-title { font-size: 12px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .5px; }

.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-panel);
  color: var(--text-secondary);
  transition: all .15s;
  font-size: 12px;
}
.tool-btn:hover { background: var(--bg-hover); border-color: var(--text-muted); }
.tool-btn.active { background: var(--accent-light); border-color: var(--accent); color: var(--accent); }

.toggle-list { display: flex; flex-direction: column; gap: 8px; }
.toggle-item { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.toggle-item input[type="checkbox"] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ===== 画布区域 ===== */
.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-canvas);
  position: relative;
}
.canvas-wrapper {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.canvas-wrapper canvas {
  position: absolute;
  cursor: crosshair;
  touch-action: none;
}
#overlayCanvas { pointer-events: none; }

/* 底部状态栏 */
.status-bar {
  height: var(--status-h);
  background: var(--bg-panel);
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.status-left, .status-right { display: flex; align-items: center; gap: 12px; }
.status-item { white-space: nowrap; }
.zoom-controls { display: flex; align-items: center; gap: 4px; }
.zoom-btn {
  width: 24px; height: 24px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
  color: var(--text-secondary);
}
.zoom-btn:hover { background: var(--bg-hover); }
.zoom-level { min-width: 40px; text-align: center; font-size: 12px; }

/* ===== 右侧色板 ===== */
.right-panel {
  width: var(--right-w);
  background: var(--bg-panel);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.current-color-box {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid var(--border-light);
}
.current-color-swatch {
  width: 40px; height: 40px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border-color);
  box-shadow: var(--shadow-sm);
}
.current-color-code { font-weight: 700; font-size: 16px; }
.current-color-hex { font-size: 12px; color: var(--text-secondary); font-family: 'Courier New', monospace; }

.palette-search {
  padding: 8px 12px;
  position: relative;
  border-bottom: 1px solid var(--border-light);
}
.palette-search input {
  width: 100%;
  padding: 6px 8px 6px 30px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.palette-search input:focus { outline: none; border-color: var(--accent); }
.search-icon { position: absolute; left: 20px; top: 50%; transform: translateY(-50%); color: var(--text-muted); }

.group-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  overflow-x: auto;
  border-bottom: 1px solid var(--border-light);
  -webkit-overflow-scrolling: touch;
}
.group-tab {
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 12px;
  white-space: nowrap;
  color: var(--text-secondary);
  background: var(--bg-primary);
  border: 1px solid var(--border-light);
}
.group-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.palette-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}
.palette-group { margin-bottom: 12px; }
.palette-group-title { font-size: 11px; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; padding-left: 4px; }
.palette-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  gap: 4px;
}
.color-cell {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
  cursor: pointer;
  position: relative;
  transition: transform .1s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.color-cell:hover { transform: scale(1.1); z-index: 2; }
.color-cell.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-light); }
.color-cell-code {
  font-size: 9px;
  font-weight: 700;
  text-shadow: 0 0 3px rgba(255,255,255,.8), 0 0 3px rgba(0,0,0,.3);
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s;
}
.color-cell:hover .color-cell-code { opacity: 1; }

/* 颜色统计 */
.color-stats {
  border-top: 1px solid var(--border-light);
  padding: 8px 12px;
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.stats-title { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.stats-empty { font-size: 12px; color: var(--text-muted); text-align: center; padding: 8px; }
.stats-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  font-size: 12px;
}
.stats-color-dot { width: 16px; height: 16px; border-radius: 4px; border: 1px solid var(--border-color); flex-shrink: 0; }
.stats-color-code { font-weight: 600; min-width: 32px; }
.stats-color-count { color: var(--text-secondary); margin-left: auto; }

/* ===== 模态框 ===== */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.4);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-content {
  background: var(--bg-panel);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
}
.modal-sm { max-width: 420px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-light);
}
.modal-header h2 { font-size: 18px; font-weight: 700; }
.modal-close { font-size: 20px; color: var(--text-secondary); width: 32px; height: 32px; border-radius: var(--radius-sm); display: flex; align-items: center; justify-content: center; }
.modal-close:hover { background: var(--bg-hover); }
.modal-body { padding: 20px; }

/* 上传区 */
.upload-zone {
  border: 2px dashed var(--border-color);
  border-radius: var(--radius-lg);
  padding: 48px 24px;
  text-align: center;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all .2s;
}
.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--accent);
  background: var(--accent-light);
}
.upload-zone p { margin-top: 12px; font-size: 15px; }
.upload-hint { font-size: 12px !important; color: var(--text-muted); }

/* 图片参数 */
.image-params { display: flex; flex-direction: column; gap: 16px; }
.param-row { display: flex; align-items: center; gap: 12px; }
.param-row > label { width: 80px; font-size: 13px; color: var(--text-secondary); flex-shrink: 0; }
.param-controls { flex: 1; display: flex; align-items: center; gap: 8px; }
.param-controls select {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-panel);
  font-size: 13px;
}
.slider { flex: 1; accent-color: var(--accent); }
.slider-value { min-width: 30px; text-align: center; font-size: 13px; color: var(--text-secondary); }

.image-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: center;
}
.preview-box {
  flex: 1;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px;
  text-align: center;
  background: var(--bg-primary);
}
.preview-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.preview-box canvas { max-width: 100%; height: auto; image-rendering: pixelated; border-radius: var(--radius-sm); }
.preview-arrow { font-size: 24px; color: var(--text-muted); }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* 导出选项 */
.export-options { display: flex; flex-direction: column; gap: 8px; }
.export-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  transition: all .15s;
  text-align: left;
}
.export-option:hover { border-color: var(--accent); background: var(--accent-light); }
.export-icon { font-size: 28px; }
.export-name { font-weight: 600; font-size: 14px; }
.export-desc { font-size: 12px; color: var(--text-secondary); }
.export-toggles { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border-light); display: flex; gap: 16px; }

/* ===== Toast 提示 ===== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(33,37,41,.92);
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-size: 14px;
  z-index: 2000;
  box-shadow: var(--shadow-lg);
  animation: toastIn .3s ease;
  max-width: 90vw;
  text-align: center;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ===== 移动端组件（桌面隐藏，必须在媒体查询之前） ===== */
.mobile-toolbar { display: none; }
.panel-overlay { display: none; }
.mobile-only { display: none; }

/* ===== 响应式：平板 (768px - 1024px) ===== */
@media (max-width: 1024px) {
  :root { --left-w: 72px; --right-w: 220px; }
  .tool-btn span { display: none; }
  .tool-btn { padding: 14px 8px; }
  .panel-title { display: none; }
  .toggle-item { font-size: 11px; gap: 4px; }
  .toggle-item input { width: 18px; height: 18px; }
  .btn-full span { display: none; }
  .btn-full { padding: 12px 8px; }
  .size-control label { display: none; }

  /* 模式切换：保持可见但精简为图标 */
  .mode-btn { padding: 6px 10px; }
  .mode-btn svg { width: 18px; height: 18px; }

  /* 保留 logo 文字但缩小 */
  .logo-text { font-size: 14px; }
  .logo-text small { font-size: 10px; }
}

/* ===== 响应式：手机 (< 768px) ===== */
@media (max-width: 768px) {
  :root { --topbar-h: 48px; --status-h: 32px; }

  /* ===== 顶栏：极简 ===== */
  .topbar { padding: 0 8px; gap: 4px; }
  .menu-btn { display: flex; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: var(--radius-sm); }
  .menu-btn:hover { background: var(--bg-hover); }
  .logo-text { display: none; }
  .logo-icon { font-size: 22px; }

  /* 顶栏右侧：只留色板按钮，其余移到底部操作栏 */
  .topbar-right { gap: 4px; }
  .size-control, #undoBtn, #redoBtn { display: none; }
  .topbar-center { display: none; }

  #exportBtn {
    padding: 7px 12px;
    font-size: 13px;
  }
  #exportBtn svg { width: 18px; height: 18px; }

  /* 色板按钮：手机端在顶栏显示 */
  .mobile-only { display: flex !important; }

  /* ===== 顶部操作栏（紧随顶栏下方） ===== */
  .mobile-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 52px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
    padding: 0 4px;
    flex-shrink: 0;
    z-index: 40;
    box-shadow: 0 1px 4px rgba(0,0,0,.06);
  }
  .mobile-toolbar .mt-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 10px;
    color: var(--text-secondary);
    min-width: 44px;
    min-height: 44px;
    justify-content: center;
  }
  .mobile-toolbar .mt-btn svg { width: 20px; height: 20px; }
  .mobile-toolbar .mt-btn.active { color: var(--accent); }
  .mobile-toolbar .mt-btn:active { background: var(--bg-hover); }

  /* 底部操作栏：尺寸输入迷你版 */
  .mt-size {
    display: flex;
    align-items: center;
    gap: 2px;
    font-size: 10px;
    color: var(--text-secondary);
  }
  .mt-size input {
    width: 36px;
    padding: 3px 2px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    text-align: center;
    font-size: 12px;
    background: var(--bg-panel);
  }
  .mt-size input:focus { outline: none; border-color: var(--accent); }

  /* ===== 画布区域 ===== */

  /* ===== 左侧面板（滑出） ===== */
  .main-area { position: relative; }
  .left-panel {
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 220px;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: var(--shadow-lg);
  }
  .left-panel.open { transform: translateX(0); }
  .left-panel .tool-btn span { display: inline; }
  .left-panel .tool-btn { padding: 10px 6px; }
  .left-panel .panel-title { display: block; }
  .left-panel .toggle-item { font-size: 13px; }
  .left-panel .btn-full span { display: inline; }

  /* ===== 右侧面板（滑出） ===== */
  .right-panel {
    position: absolute;
    right: 0; top: 0; bottom: 0;
    width: 88%;
    max-width: 340px;
    z-index: 60;
    transform: translateX(100%);
    transition: transform .25s ease;
    box-shadow: var(--shadow-lg);
  }
  .right-panel.open { transform: translateX(0); }

  /* ===== 面板遮罩层 ===== */
  .panel-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.35);
    z-index: 55;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease;
  }
  .panel-overlay.show {
    opacity: 1;
    pointer-events: auto;
  }

  /* ===== 状态栏 ===== */
  .status-bar { font-size: 11px; padding: 0 8px; height: var(--status-h); }
  .status-left .status-item:not(:first-child) { display: none; }
  .status-right .status-item { display: none; }
  .zoom-btn { width: 28px; height: 28px; }

  /* ===== 模态框 ===== */
  .modal-content { max-width: 100%; border-radius: var(--radius-md); }
  .modal-overlay { padding: 8px; }
  .modal-body { padding: 12px; }
  .modal-header { padding: 12px 14px; }
  .modal-header h2 { font-size: 16px; }
  .param-row { flex-direction: column; align-items: stretch; gap: 4px; }
  .param-row > label { width: auto; font-size: 12px; }
  .param-controls select { font-size: 14px; padding: 8px 10px; }
  .image-preview { flex-direction: column; }
  .preview-arrow { transform: rotate(90deg); }
  .upload-zone { padding: 32px 16px; }
  .export-option { padding: 14px 10px; }
  .export-icon { font-size: 24px; }
  .export-name { font-size: 13px; }
  .export-desc { font-size: 11px; }

  /* 增大触摸目标 */
  .color-cell { min-width: 36px; min-height: 36px; }
  .group-tab { padding: 6px 12px; font-size: 13px; }
  .btn { min-height: 36px; }
  .btn-primary { padding: 8px 14px; }

  /* 画布交互区留足空间 */
  .canvas-wrapper { touch-action: none; }
}

/* ===== 滚动条美化 ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* 防止用户选中文字（画图时） */
.canvas-wrapper { user-select: none; -webkit-user-select: none; }
