:root {
  --bg: #fdf6ec;
  --ink: #33302b;
  --muted: #8a8577;
  --accent: #ff5d8f;
  --accent-dark: #e91e63;
  --accent2: #5b8cff;
  --card: #ffffff;
  --line: #eee3d2;
  --shadow: 0 2px 10px rgba(90, 60, 30, 0.08);
}

* {
  box-sizing: border-box; margin: 0; padding: 0;
  -webkit-tap-highlight-color: transparent;
  /* 禁止长按选中/弹出拷贝菜单（游戏页面无文本选择需求） */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

html, body { height: 100%; }

body {
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow: hidden;
  user-select: none;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

#app { position: relative; height: 100vh; height: 100dvh; overflow: hidden; }

/* ---------- 画布区：铺满全屏，UI 全部浮在画布之上 ---------- */
#stage-wrap {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background-color: #ffffff;
  background-image: radial-gradient(#d0d0d0 1.2px, transparent 1.2px);
  background-size: 22px 22px;
}

#stage { position: absolute; inset: 0; touch-action: none; display: block; }
#fx { position: absolute; inset: 0; pointer-events: none; }

/* ---------- 浮层通用：容器穿透不挡画布，控件自身恢复交互 ---------- */
#topbar, #tools, #side-actions, #bottombar { position: absolute; z-index: 10; pointer-events: none; }
#topbar .cluster, .progress-wrap, #tools .tool, #side-actions .rbtn, #palette { pointer-events: auto; }

/* 未导入图片：只保留图库/上传/设置按钮，其余 UI 隐藏（导入后由 JS 移除 no-puzzle） */
body.no-puzzle .progress-wrap,
body.no-puzzle #btn-fit,
body.no-puzzle #btn-export,
body.no-puzzle #tools,
body.no-puzzle #side-actions { display: none; }

/* 圆形浮钮：白底 + 投影，游戏 UI 风格 */
.rbtn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 3px 12px rgba(60, 40, 20, 0.18);
  font-size: 19px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, box-shadow 0.15s;
}
.rbtn:hover { transform: scale(1.08); }
.rbtn:active { transform: scale(0.94); }

/* ---------- 顶部浮层 ---------- */
#topbar {
  top: calc(10px + env(safe-area-inset-top, 0px));
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.cluster { display: flex; gap: 8px; }

.progress-wrap {
  /* 绝对居中：不受两侧按钮组宽度差影响 */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(300px, 38vw);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 999px;
  padding: 11px 16px;
  box-shadow: 0 3px 12px rgba(60, 40, 20, 0.18);
}

.progress-wrap .track {
  flex: 1;
  height: 10px;
  border-radius: 99px;
  background: #f0e7d8;
  overflow: hidden;
}

#progress-bar {
  height: 100%;
  width: 0%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--accent), #ff9f45);
  transition: width 0.25s ease;
}

#progress-text { font-size: 12px; font-weight: 700; color: var(--muted); min-width: 38px; text-align: right; }

/* 窄屏：进度胶囊回到文档流，折到第二行居中 */
@media (max-width: 640px) {
  #topbar { flex-wrap: wrap; }
  .progress-wrap { position: static; transform: none; order: 3; margin: 0 auto; width: min(300px, 72vw); }
}

#empty-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.hint-card {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: var(--shadow);
  line-height: 1.9;
}

.hint-card .big { font-size: 40px; margin-bottom: 8px; }
.hint-card .sub { color: var(--muted); font-size: 13px; }

/* ---------- 左侧工具浮钮（笔刷/颜料桶/橡皮/取色） ---------- */
#tools {
  left: 10px;
  bottom: 112px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tool {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 3px 12px rgba(60, 40, 20, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, box-shadow 0.15s;
}

.tool .ticon { font-size: 21px; }
.tool:hover { transform: scale(1.08); }
.tool:active { transform: scale(0.94); }
/* 选中工具：accent 色描边环（用 box-shadow 不改变盒尺寸，避免位置偏移） */
.tool.active { box-shadow: 0 0 0 3px var(--accent), 0 3px 12px rgba(60, 40, 20, 0.18); }

/* ---------- 右侧操作浮钮（撤销/重做/提示） ---------- */
#side-actions {
  right: 10px;
  bottom: 112px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---------- 底部悬浮色板胶囊（固定宽度，内容不足时居中） ---------- */
#bottombar {
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  /* 固定宽度：色号涂完移除后胶囊尺寸不变，不随内容自适应 */
  width: 640px;
  max-width: calc(100vw - 20px);
}

#palette {
  display: flex;
  align-items: center; /* 色卡垂直居中：小屏色卡变矮时也不偏上 */
  /* 数量不足时水平居中；溢出时 safe 保证从左侧起滚，不裁切 */
  justify-content: safe center;
  gap: 8px;
  width: 100%;
  /* 上下 padding 预留选中态圆环外扩空间，保证色卡位置固定不偏移 */
  padding: 10px 14px;
  overflow-x: auto;
  min-height: 74px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(60, 40, 20, 0.22);
  scrollbar-width: none;
}
#palette::-webkit-scrollbar { display: none; }

/* 双伪元素 auto 外边距：色卡不足一屏时整体居中，溢出时正常滚动 */
#palette::before { content: ""; margin: auto; flex: 0 0 0; }
#palette:not(:empty)::after { content: ""; margin: auto; flex: 0 0 0; }

.swatch {
  position: relative;
  flex: 0 0 auto;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08), var(--shadow);
  transition: transform 0.12s, box-shadow 0.12s, opacity 0.15s;
}

.swatch .num { font-size: 17px; font-weight: 800; text-shadow: 0 1px 2px rgba(0,0,0,0.15); }

/* 未选中色卡：色块外缘细弧显示涂色进度（--p 为 0~1，--prog 为同色系加深色），贴着外圈不留缝 */
.swatch .prog {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: conic-gradient(var(--prog, rgba(255,255,255,0.85)) calc(var(--p, 0) * 1turn), transparent 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3px));
  pointer-events: none;
}

/* 选中色卡用外圈圆环进度条，边缘细弧隐藏 */
.swatch.active .prog { display: none; }

/* 选中与悬停不再上下移动：位置固定，选中态仅靠圆环进度条 + 阴影区分 */
.swatch.active {
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08), 0 6px 14px rgba(0, 0, 0, 0.18);
}

/* 选中色号的圆环进度条：--p 为 0~1 进度，--ring 为同色系深色；环内侧贴色卡外缘不留缝 */
.swatch.active::before {
  content: "";
  position: absolute;
  inset: -4.5px;
  border-radius: 50%;
  background: conic-gradient(var(--ring, #333) calc(var(--p, 0) * 1turn), rgba(0, 0, 0, 0.12) 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4.5px), #000 calc(100% - 3.5px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 4.5px), #000 calc(100% - 3.5px));
  pointer-events: none;
}

/* 色号涂完：先弹出打勾，再缩小淡出（动画结束后 JS 将其从列表移除） */
.swatch.just-done { pointer-events: none; animation: swatch-done-out 0.78s ease forwards; }
.swatch.just-done .num, .swatch.just-done .prog { visibility: hidden; }
.swatch.just-done::after {
  content: "✓";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
  animation: swatch-check-pop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes swatch-check-pop {
  0% { transform: scale(0); }
  100% { transform: scale(1); }
}

@keyframes swatch-done-out {
  0%, 62% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.4); }
}

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(50, 38, 25, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  touch-action: none; /* 遮罩上屏蔽浏览器默认触摸手势 */
}

.modal.hidden { display: none; }

.card {
  background: var(--card);
  border-radius: 20px;
  padding: 20px 22px;
  width: min(94vw, 560px);
  max-height: 88vh;
  overflow: auto;
  box-shadow: 0 12px 40px rgba(50, 30, 10, 0.25);
  touch-action: pan-y; /* 卡片内容允许纵向滚动 */
}

.card.center { text-align: center; width: min(94vw, 420px); }
.card h2 { font-size: 19px; }

.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }

.close {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: #f2ece1;
  font-size: 14px;
}
.close:hover { background: #e8dfce; }

#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
}

.gcard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border-radius: 16px;
  border: 2px solid var(--line);
  background: #fffdf8;
  font-size: 13px;
  font-weight: 700;
  transition: border-color 0.15s, transform 0.12s;
}

.gcard:hover { border-color: var(--accent); transform: translateY(-2px); }

.gthumb {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 10px;
  background: #faf6ee;
  image-rendering: pixelated;
}

.pixel { image-rendering: pixelated; }

.upload-body { display: flex; gap: 18px; flex-wrap: wrap; }

#upload-preview {
  width: 180px;
  height: 180px;
  object-fit: contain;
  border-radius: 12px;
  background: #faf6ee;
  border: 1px solid var(--line);
  flex: 0 0 auto;
}

.form { display: flex; flex-direction: column; gap: 12px; flex: 1 1 200px; font-size: 14px; }

.form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.form label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }

.form select {
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fffdf8;
  padding: 0 10px;
  font-size: 14px;
  font-family: inherit;
}

.form input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.form hr { border: none; border-top: 1px solid var(--line); }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.center-row { justify-content: center; margin-top: 14px; }

.cta {
  padding: 11px 20px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 800;
  transition: transform 0.1s, filter 0.15s;
}
.cta:hover { filter: brightness(1.06); transform: translateY(-1px); }
.cta.secondary { background: var(--accent2); }
.cta.danger { background: #ef5350; }
.cta.ghost { background: #f2ece1; color: var(--ink); }

.tip { margin-top: 12px; font-size: 12px; color: var(--muted); }

#complete-preview {
  height: 220px;
  width: auto;
  max-width: 100%;
  margin: 16px auto 0;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--line);
  display: block;
}

#export-img {
  width: min(280px, 72vw);
  margin: 4px auto 14px;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--line);
  display: block;
}

a.cta { text-decoration: none; display: inline-block; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 150px;
  transform: translateX(-50%) translateY(10px);
  background: rgba(35, 28, 22, 0.88);
  color: #fff;
  padding: 10px 18px;
  border-radius: 99px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
  white-space: nowrap;
}

#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 小屏适配 ---------- */
@media (max-width: 720px) {
  .rbtn { width: 42px; height: 42px; font-size: 17px; }
  .tool { width: 44px; height: 44px; }
  .tool .ticon { font-size: 19px; }
  .swatch { width: 46px; height: 46px; }
  .swatch .num { font-size: 15px; }
}
