:root {
  --bg: #eceff4;
  --panel: #f6f8fb;
  --card: #ffffff;
  --card-2: #eef1f6;
  --line: #e4e8ef;
  --text: #1d2330;
  --muted: #707887;
  --accent: #ff5d4e;
  --accent-2: #ff7d5e;
  --accent-d: #ec4636;
  --accent-soft: rgba(255, 93, 78, 0.12);
  --ok: #12b981;
  --radius: 14px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px -10px rgba(16, 24, 40, 0.14);
  --on-dark: rgba(255, 255, 255, 0.62);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased;
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- top bar ---------- */
#topbar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  height: 58px; flex: 0 0 58px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); z-index: 7;
}
#topbar .brand { display: flex; align-items: center; gap: 10px; }
#topbar .logo {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center;
  font-size: 21px; color: #fff; font-weight: 700;
  background: linear-gradient(140deg, var(--accent-2), var(--accent-d));
  box-shadow: 0 6px 16px -6px var(--accent), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  transition: transform .25s cubic-bezier(.2, .8, .3, 1.4);
}
#topbar .logo:hover { transform: rotate(-6deg) scale(1.06); }
#topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }

.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; display: block; flex: 0 0 auto; }

.tools { display: flex; gap: 7px; justify-self: center; }
.tooldiv { width: 1px; height: 24px; background: var(--line); margin: 0 4px; flex: 0 0 auto; align-self: center; }
.tool {
  display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 13px;
  font: inherit; font-size: 13px; font-weight: 600; border-radius: 11px; color: var(--text);
  background: var(--card); border: 1px solid var(--line); cursor: pointer; transition: .15s;
}
.tool:hover { border-color: var(--accent); color: var(--accent); transform: translateY(-1px); }
.tool.active {
  background: linear-gradient(140deg, var(--accent-2), var(--accent-d)); border-color: transparent;
  color: #fff; box-shadow: 0 8px 16px -8px var(--accent);
}

.topactions { display: flex; gap: 8px; align-items: center; justify-self: end; }
.icon { width: 40px; height: 40px; padding: 0; flex: 0 0 auto; display: grid; place-items: center; }
#exportBtn { display: inline-flex; align-items: center; gap: 8px; padding: 0 16px; height: 40px; }

/* ---------- options strip ---------- */
#optionsbar {
  display: flex; align-items: center; gap: 16px; min-height: 54px; flex: 0 0 auto;
  padding: 8px 18px; background: var(--card); border-bottom: 1px solid var(--line); z-index: 6;
  justify-content: center;
}
.opt { display: flex; align-items: center; gap: 12px; }
.opt[hidden] { display: none; }
.optlabel { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.opt input[type="range"] { width: 190px; }
.slim { flex: 0 0 auto; width: auto; padding: 8px 13px; }
.sizedot {
  display: inline-block; width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent-soft); border: 2px solid var(--accent); transition: width .08s, height .08s;
}

.palette { display: flex; gap: 7px; align-items: center; }
.palette.hide { display: none; }
.pal { width: 26px; height: 26px; border-radius: 8px; cursor: pointer; transition: .12s; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12); }
.pal:hover { transform: translateY(-2px) scale(1.05); }
.pal.sel { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--accent); }
.pal.add { display: grid; place-items: center; background: transparent; box-shadow: inset 0 0 0 1.5px #c7cedb; color: var(--muted); font-size: 17px; font-weight: 700; line-height: 1; }
.pal.add:hover { box-shadow: inset 0 0 0 1.5px var(--accent); color: var(--accent); transform: translateY(-2px); }
.pal { position: relative; }
.pal .del { position: absolute; top: -6px; right: -6px; width: 15px; height: 15px; border-radius: 50%; background: #1d2330; color: #fff; font-size: 11px; line-height: 15px; text-align: center; cursor: pointer; opacity: 0; transition: opacity .12s; }
.pal:hover .del { opacity: 1; }
.pal .del:hover { background: var(--accent-d); }
.axes { display: flex; gap: 5px; }
.axes button { flex: 0 0 auto; width: 34px; padding: 7px 0; font-size: 12.5px; font-weight: 700; border-radius: 9px; }
.axes button.on { background: linear-gradient(140deg, var(--accent-2), var(--accent-d)); border-color: transparent; color: #fff; box-shadow: 0 6px 12px -6px var(--accent); }
#fillModes button { width: auto; padding: 7px 12px; } /* word chips, not X/Y/Z letters */
.check[hidden] { display: none; } /* flex display otherwise defeats the hidden attribute */

/* ---------- main: workflow + stage ---------- */
#main { flex: 1 1 auto; display: flex; min-height: 0; }
#left {
  width: 312px; flex: 0 0 312px; overflow-y: auto; padding: 14px;
  background: var(--panel); border-right: 1px solid var(--line);
}
#stage { flex: 1 1 auto; position: relative; min-width: 0; }
#left::-webkit-scrollbar { width: 10px; }
#left::-webkit-scrollbar-thumb { background: #d7dde6; border-radius: 10px; border: 3px solid var(--panel); }

/* ---------- cards & components ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 15px; margin-bottom: 13px; box-shadow: var(--shadow);
}
.card h2 {
  font-size: 14px; font-weight: 650; margin: 0 0 4px; display: flex; align-items: center; gap: 9px;
}
.step {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 7px;
  font-size: 12px; font-weight: 700; color: #fff; flex: 0 0 auto;
  background: linear-gradient(140deg, var(--accent-2), var(--accent-d));
  box-shadow: 0 4px 10px -4px var(--accent);
}
.muted { color: var(--muted); font-size: 12.5px; }
.hint { color: var(--muted); font-size: 12.5px; line-height: 1.5; margin: 6px 0 12px; }
b { color: var(--text); font-weight: 650; }

.filebtn { position: relative; display: block; margin-top: 10px; }
.filebtn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.filebtn span {
  display: block; text-align: center; padding: 14px; border: 1.5px dashed #c7cedb;
  border-radius: 12px; color: var(--text); font-weight: 600; cursor: pointer; transition: .15s; background: #fbfcfe;
}
.filebtn:hover span { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
#fileInfo { margin-top: 11px; line-height: 1.55; }

#objectsList { list-style: none; margin: 6px 0 0; padding: 0; }
#objectsList .objrow { display: flex; align-items: center; gap: 11px; padding: 7px 6px; border-radius: 9px; cursor: pointer; transition: background .15s; }
#objectsList .objrow:hover { background: var(--card-2); }
#filamentList { list-style: none; margin: 6px 0 0; padding: 0; }
#filamentList li { display: flex; align-items: center; gap: 11px; padding: 7px 6px; border-radius: 9px; transition: background .15s; }
#filamentList li:hover { background: var(--card-2); }
.swatch { width: 19px; height: 19px; border-radius: 6px; flex: 0 0 auto; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12); }
.fname { flex: 1 1 auto; }
.fcount { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }

/* switch */
#filamentList input[type="checkbox"] {
  appearance: none; -webkit-appearance: none; order: 3; flex: 0 0 auto; width: 38px; height: 22px;
  border-radius: 999px; background: #cfd6e1; position: relative; cursor: pointer; transition: background .2s; margin-left: 4px;
}
#filamentList input[type="checkbox"]::before {
  content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); transition: transform .2s;
}
#filamentList input[type="checkbox"]:checked { background: linear-gradient(140deg, var(--accent-2), var(--accent-d)); }
#filamentList input[type="checkbox"]:checked::before { transform: translateX(16px); }

.row { display: flex; align-items: center; gap: 11px; margin: 10px 0 4px; }
.row span { flex: 0 0 74px; font-size: 12.5px; color: var(--muted); }
.row input[type="range"] { flex: 1 1 auto; }
input[type="range"] {
  -webkit-appearance: none; appearance: none; height: 6px; border-radius: 4px; background: var(--card-2); outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; cursor: pointer; border: 2px solid var(--accent); box-shadow: 0 1px 4px rgba(16, 24, 40, 0.2); transition: box-shadow .15s;
}
input[type="range"]::-webkit-slider-thumb:hover { box-shadow: 0 0 0 6px var(--accent-soft); }
input[type="number"] {
  width: 70px; flex: 0 0 auto; background: #fff; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
  font: inherit; font-size: 13px; font-variant-numeric: tabular-nums; text-align: right;
  -moz-appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"]:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* labelled field (e.g. Patch size) */
.field { margin: 12px 0 4px; }
.field > label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.field-row { display: flex; align-items: center; gap: 11px; }
.field-row input[type="range"] { flex: 1 1 auto; min-width: 0; }

.check { display: flex; align-items: center; gap: 9px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

.btnrow { display: flex; gap: 9px; margin-top: 10px; }
button {
  font: inherit; border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 10px 13px; border-radius: 11px; cursor: pointer; flex: 1 1 auto; transition: .15s; font-weight: 600;
}
button:hover { border-color: #c7cedb; background: #fafbfd; }
button:active { transform: scale(.97); }
button.primary {
  background: linear-gradient(140deg, var(--accent-2), var(--accent-d)); border-color: transparent;
  color: #fff; box-shadow: 0 10px 20px -10px var(--accent);
}
button.primary:hover { filter: brightness(1.05); border-color: transparent; transform: translateY(-1px); }
button.primary:disabled { filter: grayscale(.4); }
button.secondary { background: #fff; }
button.secondary:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover { background: var(--card-2); color: var(--text); }
button:disabled { opacity: .42; cursor: not-allowed; transform: none; }

.rotgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.rotgrid button { font-weight: 600; }
.swatchnote { margin-top: 10px; font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.swatchnote .swatch { width: 22px; height: 22px; }

#previewInfo { margin-top: 11px; line-height: 1.5; }
#histInfo { font-variant-numeric: tabular-nums; }
.foot { margin-top: 16px; text-align: center; font-size: 11.5px; color: var(--muted); }

/* ---------- stage ---------- */
#stage { background: radial-gradient(125% 100% at 50% 8%, #eaedf2 0%, #c9cfd9 55%, #aeb5c1 100%); }
#stage.dark { background: radial-gradient(125% 95% at 50% -5%, #2a3346 0%, #161b26 50%, #0e1118 100%); }
#viewer { position: absolute; inset: 0; }
#viewer canvas { display: block; }

.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.overlay.hide { display: none; }
.drop-hint { text-align: center; color: #6c7480; position: relative; }
.drop-hint::before {
  content: "彩"; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -58%);
  font-size: 230px; line-height: 1; color: var(--accent); opacity: .07; pointer-events: none; z-index: -1;
}
.drop-hint .big { font-size: 25px; color: #2c333f; margin-bottom: 9px; font-weight: 650; }
#stage.dark .drop-hint { color: var(--on-dark); }
#stage.dark .drop-hint .big { color: #fff; }
#stage.dark .drop-hint::before { color: #fff; opacity: .05; }
#stage.dragover .overlay { background: rgba(255, 93, 78, 0.1); outline: 2px dashed var(--accent); outline-offset: -18px; }

.toast {
  position: absolute; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: rgba(20, 24, 33, 0.9); color: #fff; padding: 11px 20px; border-radius: 999px;
  font-size: 13px; pointer-events: none; max-width: 80%; border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 12px 30px -10px rgba(0, 0, 0, 0.6); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
.toast.err { background: rgba(150, 32, 32, 0.92); }

.stagebtn {
  position: absolute; right: 16px; width: 42px; height: 42px; flex: 0; padding: 0; font-size: 18px; color: #3a4150;
  background: rgba(255, 255, 255, 0.72); border: 1px solid rgba(0, 0, 0, 0.08); border-radius: 12px;
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); box-shadow: 0 4px 14px -6px rgba(16, 24, 40, 0.25);
}
.stagebtn:hover { border-color: var(--accent); color: var(--accent); background: rgba(255, 255, 255, 0.92); }
#reframeBtn { top: 16px; }
#bgToggle { top: 66px; }
#stage.dark .stagebtn { background: rgba(20, 24, 33, 0.55); border-color: rgba(255, 255, 255, 0.14); color: #fff; }
#stage.dark .stagebtn:hover { background: rgba(20, 24, 33, 0.82); border-color: var(--accent); color: var(--accent); }
#orientBtn { top: 116px; }
.orientpop {
  position: absolute; right: 70px; top: 108px; z-index: 7; padding: 10px 12px;
  background: rgba(255, 255, 255, 0.96); border: 1px solid rgba(0, 0, 0, 0.08); border-radius: 12px;
  box-shadow: 0 12px 30px -10px rgba(16, 24, 40, 0.35); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
.orientpop[hidden] { display: none; }
.orientpop-label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.orientpop .rotgrid button { padding: 8px 0; }
#stage.dark .orientpop { background: rgba(20, 24, 33, 0.85); border-color: rgba(255, 255, 255, 0.14); color: #fff; }
#reframeBtn:active { transform: rotate(-90deg); }

/* ---------- animations ---------- */
@keyframes fadeInUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { opacity: 0; transform: scale(.5); } 70% { transform: scale(1.12); } 100% { opacity: 1; transform: scale(1); } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 14px) scale(.96); } to { opacity: 1; transform: translate(-50%, 0) scale(1); } }
@keyframes floaty { 0%, 100% { transform: translate(-50%, -58%) scale(1); opacity: .045; } 50% { transform: translate(-50%, -62%) scale(1.04); opacity: .085; } }

#left section.card { animation: fadeInUp .5s cubic-bezier(.2, .7, .3, 1) both; }
#left section:nth-of-type(1) { animation-delay: .03s; }
#left section:nth-of-type(2) { animation-delay: .08s; }
#left section:nth-of-type(3) { animation-delay: .13s; }
#left section:nth-of-type(4) { animation-delay: .18s; }
#left section:nth-of-type(5) { animation-delay: .23s; }
#optionsbar .opt:not([hidden]) { animation: fadeInUp .28s ease both; }
.step { animation: pop .45s cubic-bezier(.2, .8, .3, 1.4) both; }
.toast:not([hidden]) { animation: toastIn .3s cubic-bezier(.2, .9, .25, 1.2); }
.drop-hint::before { animation: floaty 7s ease-in-out infinite; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- export modal ---------- */
.modal { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; background: rgba(15, 18, 24, 0.45); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.modal[hidden] { display: none; }
.modal-card { width: min(360px, 92vw); display: flex; flex-direction: column; gap: 12px; padding: 20px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 24px 60px -20px rgba(16, 24, 40, 0.5); }
.modal-card h2 { margin: 0; font-size: 16px; }
.modal-card .btnrow { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
#exportFmt button { width: auto; padding: 7px 12px; } /* word chips (3MF/OBJ/Split), not X/Y/Z letters */
#exportFmt button[disabled] { opacity: .4; cursor: not-allowed; }
#exportDesc { margin: 0; }
#exportName { width: 100%; background: #fff; color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font: inherit; font-size: 13px; }
#exportName:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
