/* 未来3D-像素积木工坊 —— 糖果积木主题
   设计语言：奶油纸底 + 波点、深色粗描边、硬阴影、
   按钮按下去会"压扁"，每个工具一个专属糖果色。 */
* { box-sizing: border-box; }

:root {
  --ink: #2b2850;          /* 描边深色 */
  --paper: #fffdf7;
  --ink-soft: #6b6790;
  --yellow: #ffd93d;
  --coral: #ff6b6b;
  --sky: #4fc3f7;
  --mint: #35d0a5;
  --pink: #ff8fab;
  --violet: #9b8cff;
  --orange: #ffb84d;
  --radius: 16px;
}

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  font-family: ui-rounded, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--ink);
  background-color: #fff8ec;
  /* 双色波点糖果桌布，透出 3D 场景背后 */
  background-image:
    radial-gradient(rgba(255, 217, 61, 0.35) 4px, transparent 4.5px),
    radial-gradient(rgba(79, 195, 247, 0.28) 4px, transparent 4.5px);
  background-size: 46px 46px;
  background-position: 0 0, 23px 23px;
  user-select: none;
  -webkit-user-select: none;
}

button { font-family: inherit; cursor: pointer; }

/* 通用"积木按钮"：粗描边 + 硬阴影，按下压扁 */
.chunky {
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 3px 3px 0 var(--ink);
  font-weight: 800;
  color: var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
}
.chunky:hover { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 var(--ink); }
.chunky:active { transform: translate(3px, 3px); box-shadow: 0 0 0 var(--ink); }
.chunky:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: 3px 3px 0 var(--ink); }
.chunky:disabled:active { transform: none; }

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--yellow);
  border-bottom: 3px solid var(--ink);
  flex: 0 0 auto;
  z-index: 20;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.logo-name {
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.5px;
  white-space: nowrap;
}
.logo-name a { color: inherit; text-decoration: none; }
.logo-name a:hover { text-decoration: underline; text-decoration-thickness: 2.5px; text-underline-offset: 5px; }
.logo-name em,
.load-title em {
  font-style: normal;
  color: #fff;
  background: var(--coral);
  border: 2.5px solid var(--ink);
  border-radius: 8px;
  padding: 0 5px 1px;
  display: inline-block;
  line-height: 1.15;
  box-shadow: 2px 2px 0 rgba(43, 40, 80, 0.35);
  transition: transform 0.15s;
}
.logo-name a:hover em { transform: rotate(-2deg) scale(1.05); }

/* ---------- 加载遮罩 ---------- */
#loadingOverlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff8ec;
  background-image:
    radial-gradient(rgba(255, 217, 61, 0.35) 4px, transparent 4.5px),
    radial-gradient(rgba(79, 195, 247, 0.28) 4px, transparent 4.5px);
  background-size: 46px 46px;
  background-position: 0 0, 23px 23px;
  transition: opacity 0.4s;
}
#loadingOverlay.done { opacity: 0; pointer-events: none; }
.load-box { text-align: center; padding: 20px; }
.load-blocks { font-size: 44px; line-height: 1; margin-bottom: 16px; }
.load-blocks span { display: inline-block; animation: blockJump 1s ease-in-out infinite; }
.load-blocks span:nth-child(2) { animation-delay: 0.15s; }
.load-blocks span:nth-child(3) { animation-delay: 0.3s; }
@keyframes blockJump {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  40% { transform: translateY(-16px) rotate(-8deg); }
}
.load-title { font-size: 22px; font-weight: 900; letter-spacing: 0.5px; }
.load-bar {
  width: 250px;
  height: 16px;
  margin: 18px auto 12px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  box-shadow: 3px 3px 0 rgba(43, 40, 80, 0.3);
  overflow: hidden;
}
.load-fill {
  height: 100%;
  width: 38%;
  border-radius: 999px;
  background: repeating-linear-gradient(45deg, var(--coral) 0 10px, var(--yellow) 10px 20px);
  animation: loadSlide 1.1s linear infinite;
  transition: width 0.3s;
}
@keyframes loadSlide {
  from { transform: translateX(-110%); }
  to { transform: translateX(360%); }
}
.load-text { font-size: 13px; font-weight: 700; color: var(--ink-soft); }
.load-copyright {
  margin-top: 22px;
  font-size: 11.5px;
  font-weight: 700;
  color: rgba(43, 40, 80, 0.55);
  letter-spacing: 0.5px;
}
.load-copyright a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1.5px dotted rgba(43, 40, 80, 0.4);
}
.load-copyright a:hover { color: var(--coral); border-bottom-color: var(--coral); }
.count-chip {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 10;
  font-size: 13px;
  font-weight: 900;
  color: var(--ink);
  background: rgba(255, 253, 247, 0.92);
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  padding: 4px 12px;
  white-space: nowrap;
  box-shadow: 2.5px 2.5px 0 rgba(43, 40, 80, 0.3);
  pointer-events: none;
}

.header-actions { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; justify-content: flex-end; }

.header-btn, .header-select {
  font-size: 14px;
  padding: 7px 12px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 3px 3px 0 var(--ink);
  font-weight: 800;
  color: var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, opacity 0.15s;
}
.header-btn:hover:not(:disabled), .header-select:hover {
  transform: translate(-1px, -1px);
  box-shadow: 4px 4px 0 var(--ink);
}
.header-btn:active:not(:disabled) { transform: translate(3px, 3px); box-shadow: 0 0 0 var(--ink); }
.header-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.header-btn.icon-only { font-size: 16px; padding: 7px 9px; }

/* ---------- 主体三栏 ---------- */
#app { flex: 1; display: flex; min-height: 0; }

/* 工具栏 */
#toolbar {
  flex: 0 0 92px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 10px;
  background: rgba(255, 253, 247, 0.88);
  border-right: 3px solid var(--ink);
  overflow-y: auto;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 4px 8px;
  font-size: 12.5px;
  font-weight: 900;
  color: var(--ink-soft);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 3.5px 3.5px 0 var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
}
.tool-icon {
  font-size: 22px;
  line-height: 1;
  padding: 6px 10px;
  border: 2.5px solid var(--ink);
  border-radius: 12px;
  background: #f4f2ff;
}
.tool-btn[data-tool="add"] .tool-icon { background: var(--orange); }
.tool-btn[data-tool="paint"] .tool-icon { background: var(--pink); }
.tool-btn[data-tool="erase"] .tool-icon { background: #7bdff2; }
.tool-btn[data-tool="pick"] .tool-icon { background: var(--violet); }

.tool-btn:hover { transform: translate(-1px, -1px) rotate(-1deg); box-shadow: 4.5px 4.5px 0 var(--ink); }
.tool-btn:active { transform: translate(3.5px, 3.5px); box-shadow: 0 0 0 var(--ink); }

.tool-btn.active { color: var(--ink); }
.tool-btn.active[data-tool="add"] { background: #ffe9c7; }
.tool-btn.active[data-tool="paint"] { background: #ffe0e8; }
.tool-btn.active[data-tool="erase"] { background: #d9f6fa; }
.tool-btn.active[data-tool="pick"] { background: #eceaff; }

/* 3D 视口 */
#viewport {
  flex: 1;
  position: relative;
  min-width: 0;
  overflow: hidden;
}
#viewport canvas { display: block; }

#hint {
  position: absolute;
  left: 14px;
  bottom: 14px;
  font-size: 12.5px;
  font-weight: 800;
  color: var(--ink);
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  padding: 6px 14px;
  box-shadow: 3px 3px 0 var(--ink);
  pointer-events: none;
  max-width: calc(100% - 210px);
}

/* 右下角操作区：拍照 + 导出 STL */
.vp-actions {
  position: absolute;
  right: 18px;
  bottom: 18px;
  display: flex;
  align-items: stretch;
  gap: 10px;
}
.vp-actions .header-btn { display: flex; align-items: center; }

#exportBtn {
  font-size: 17px;
  font-weight: 900;
  color: var(--ink);
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: 18px;
  padding: 14px 22px;
  box-shadow: 5px 5px 0 var(--ink);
  animation: bob 2.6s ease-in-out infinite;
}
#exportBtn:hover { animation-play-state: paused; transform: translate(-1px, -2px); box-shadow: 7px 7px 0 var(--ink); }
#exportBtn:active { animation: none; transform: translate(5px, 5px); box-shadow: 0 0 0 var(--ink); }

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-1deg); }
  50% { transform: translateY(-4px) rotate(1deg); }
}

/* 调色盘 */
#palette-panel {
  flex: 0 0 132px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 14px 10px;
  background: rgba(255, 253, 247, 0.88);
  border-left: 3px solid var(--ink);
  overflow-y: auto;
}

#currentColor {
  width: 54px;
  height: 54px;
  border-radius: 16px;
  background: #ef4444;
  border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 var(--ink), inset 0 -6px 0 rgba(0, 0, 0, 0.12);
}

#palette {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  justify-items: center;
}

.swatch {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 3px solid var(--ink);
  box-shadow: 2.5px 2.5px 0 var(--ink);
  padding: 0;
  transition: transform 0.1s;
}
.swatch:hover { transform: scale(1.16) rotate(-7deg); }
.swatch.active {
  box-shadow: 0 0 0 3px var(--paper), 0 0 0 6px var(--coral), 2.5px 2.5px 0 var(--ink);
  transform: scale(1.06);
}

#customWrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 900;
  color: var(--ink-soft);
  text-align: center;
}
#customColor {
  width: 48px;
  height: 34px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--paper);
  padding: 2px;
  cursor: pointer;
}

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(43, 40, 80, 0.5);
}
.modal[hidden] { display: none !important; }

.modal-card {
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 20px;
  box-shadow: 9px 9px 0 rgba(43, 40, 80, 0.4);
  padding: 0 16px 12px;
  max-width: min(94vw, 680px);
  max-height: 94vh;
  overflow: auto;
}
.modal-card.small { width: 350px; padding-top: 0; }

.modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 900;
  background: var(--yellow);
  border-bottom: 3px solid var(--ink);
  margin: 0 -16px 12px;
  padding: 11px 16px;
  border-radius: 16px 16px 0 0;
}
.modal-card.small .modal-head { border-radius: 16px 16px 0 0; margin-top: 0; }
.modal-sub { font-size: 13px; font-weight: 600; color: var(--ink-soft); flex: 1; }
.modal-head b { flex-shrink: 0; }
.modal-head .icon-only { margin-left: auto; }
.modal-text { font-size: 14px; color: var(--ink-soft); margin: 4px 0 14px; line-height: 1.7; font-weight: 600; }

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 9px;
  margin-top: 10px;
  flex-wrap: wrap;
}

#boardCanvas {
  display: block;
  /* 高度跟随视口，保证弹窗不出现上下滚动条 */
  width: max(260px, min(576px, 82vw, calc(100vh - 320px)));
  height: auto;
  aspect-ratio: 1;
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 4px 4px 0 rgba(43, 40, 80, 0.25);
  touch-action: none;
  cursor: crosshair;
}

/* 画板内的调色盘（弹窗会挡住主调色盘，所以画板里也要有一份） */
.board-palette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-top: 10px;
}
.board-palette .swatch { width: 26px; height: 26px; border-radius: 8px; box-shadow: 2px 2px 0 var(--ink); }

/* ---------- 作品库 ---------- */
.save-row {
  display: flex;
  gap: 10px;
}
.save-row input {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 9px 12px;
  outline: none;
}
.save-row input:focus { box-shadow: 3px 3px 0 var(--sky); }
.save-row input::placeholder { color: #a5a1c4; }

.saves-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
  max-height: 250px;
  overflow: auto;
}
.save-item {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 2.5px solid var(--ink);
  border-radius: 12px;
  padding: 8px 12px;
  background: #fffaf0;
  box-shadow: 2.5px 2.5px 0 rgba(43, 40, 80, 0.18);
}
.save-name {
  font-weight: 900;
  font-size: 14px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.save-meta {
  font-size: 11.5px;
  color: var(--ink-soft);
  font-weight: 600;
  white-space: nowrap;
}
.save-empty {
  text-align: center;
  color: var(--ink-soft);
  font-weight: 600;
  padding: 12px 0;
}

button.mini {
  font-size: 13px;
  font-weight: 900;
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 10px;
  padding: 7px 13px;
  box-shadow: 2.5px 2.5px 0 var(--ink);
  color: var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
}
button.mini:hover { transform: translate(-1px, -1px); box-shadow: 3.5px 3.5px 0 var(--ink); }
button.mini:active { transform: translate(2.5px, 2.5px); box-shadow: 0 0 0 var(--ink); }
button.mini.active { color: #fff; background: var(--coral); }
button.mini.icon-only { font-size: 15px; padding: 6px 9px; }

button.primary {
  font-size: 14.5px;
  font-weight: 900;
  color: #fff;
  background: var(--mint);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 9px 16px;
  box-shadow: 3.5px 3.5px 0 var(--ink);
  text-shadow: 0 1.5px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.08s, box-shadow 0.08s;
}
button.primary:hover { transform: translate(-1px, -1px); box-shadow: 4.5px 4.5px 0 var(--ink); }
button.primary:active { transform: translate(3.5px, 3.5px); box-shadow: 0 0 0 var(--ink); }
button.primary.danger { background: var(--coral); }

.thickness-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 900;
  color: var(--ink-soft);
  margin-right: auto;
}
.thickness-label select {
  font-family: inherit;
  font-size: 13px;
  font-weight: 900;
  border: 2.5px solid var(--ink);
  border-radius: 9px;
  padding: 5px 6px;
  background: var(--paper);
  color: var(--ink);
}
.modal-tip { font-size: 12px; color: var(--ink-soft); margin: 8px 0 0; font-weight: 600; }

/* ---------- 页脚版权 ---------- */
#footer {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 16px;
  font-size: 12px;
  font-weight: 700;
  color: #ffe9a8;
  background: var(--ink);
  border-top: 3px solid var(--ink);
}
.footer-tagline { opacity: 0.75; font-weight: 600; }
#footer a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1.5px dotted rgba(255, 233, 168, 0.6);
  transition: color 0.15s;
}
#footer a:hover { color: #fff; border-bottom-color: #fff; }

/* ---------- 提示气泡 ---------- */
#toast {
  position: fixed;
  top: 74px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  font-size: 14.5px;
  font-weight: 900;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 10px 20px;
  box-shadow: 4px 4px 0 var(--ink);
  max-width: 86vw;
  text-align: center;
  animation: pop 0.22s ease-out;
}
#toast[hidden] { display: none; }

@keyframes pop {
  from { transform: translateX(-50%) scale(0.8) rotate(-2deg); opacity: 0; }
  to   { transform: translateX(-50%) scale(1) rotate(0deg);    opacity: 1; }
}

/* ---------- 窄屏适配 ---------- */
@media (max-width: 720px) {
  #app { flex-direction: column; }
  #toolbar {
    flex-direction: row;
    flex: 0 0 auto;
    order: 2;
    border-right: none;
    border-top: 3px solid var(--ink);
    padding: 8px 10px;
  }
  .tool-btn { flex: 1; flex-direction: row; padding: 7px 6px; font-size: 11px; gap: 5px; }
  .tool-icon { font-size: 17px; padding: 4px 7px; }
  #viewport { order: 1; }
  #palette-panel {
    order: 3;
    flex: 0 0 auto;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    border-left: none;
    border-top: 3px solid var(--ink);
    padding: 8px;
  }
  #currentColor { width: 38px; height: 38px; }
  #palette { grid-template-columns: repeat(8, 1fr); }
  .swatch { width: 28px; height: 28px; border-radius: 9px; }
  #customWrap { flex-direction: row; }
  #customColor { width: 36px; height: 28px; }
  #hint { display: none; }
  .footer-tagline { display: none; }
  .count-chip { top: 10px; right: 10px; font-size: 12px; padding: 3px 10px; }
  .vp-actions { right: 12px; bottom: 12px; }
  #exportBtn { font-size: 15px; padding: 12px 18px; }
  .vp-actions .header-btn { font-size: 13px; padding: 7px 10px; }
  .header-btn:not(.icon-only) { padding: 7px 8px; font-size: 13px; }
  body {
    background-image:
      radial-gradient(rgba(255, 217, 61, 0.35) 3px, transparent 3.5px),
      radial-gradient(rgba(79, 195, 247, 0.28) 3px, transparent 3.5px);
    background-size: 36px 36px;
    background-position: 0 0, 18px 18px;
  }
}
