:root {
  --bg: #f4f2ee;
  --panel: #ffffff;
  --ink: #1d1b19;
  --muted: #6d675f;
  --line: #e2ddd5;
  --accent: #c8202a;
  --accent-ink: #ffffff;
  --soft: #f8f6f2;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4b00;
  --canvas: #efeae2;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161514; --panel: #201f1d; --ink: #ece8e2; --muted: #a39d94; --line: #36332f;
    --accent: #e5484d; --soft: #2a2826; --warn-bg: #3a3017; --warn-ink: #f3d68a; --canvas: #2b2926;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161514; --panel: #201f1d; --ink: #ece8e2; --muted: #a39d94; --line: #36332f;
  --accent: #e5484d; --soft: #2a2826; --warn-bg: #3a3017; --warn-ink: #f3d68a; --canvas: #2b2926;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
.top { display: flex; align-items: baseline; gap: 16px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.top h1 { font-size: 17px; margin: 0; font-weight: 650; letter-spacing: -0.01em; }
.top h1 span { color: var(--muted); font-weight: 400; }
.status { color: var(--muted); font-size: 13px; margin-left: auto; }

.app { flex: 1; display: grid; grid-template-columns: 360px 1fr; min-height: 0; }
.panel { overflow-y: auto; background: var(--panel); border-right: 1px solid var(--line); padding: 8px 14px 24px; }
.view { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }

details { border-bottom: 1px solid var(--line); padding: 6px 0 10px; }
summary { cursor: pointer; font-weight: 600; padding: 6px 0; list-style: none; }
summary::-webkit-details-marker { display: none; }
summary::before { content: "▸"; display: inline-block; width: 14px; color: var(--muted); transition: transform .15s; }
details[open] > summary::before { transform: rotate(90deg); }
h3 { font-size: 13px; margin: 12px 0 6px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.row > label { flex: 1; color: var(--ink); font-size: 13px; }
.row.col { flex-direction: column; align-items: stretch; }
.row.check label { display: flex; gap: 6px; align-items: center; }
.row input[type=range] { width: 120px; accent-color: var(--accent); }
.row output { width: 38px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
input[type=number], select, textarea {
  background: var(--soft); color: var(--ink); border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 7px; font: inherit;
}
input[type=number] { width: 80px; }
select { max-width: 190px; }
textarea { width: 100%; resize: vertical; font-size: 15px; }
.hint { color: var(--muted); font-size: 12px; margin: 6px 0; }

button {
  font: inherit; border: 1px solid var(--line); background: var(--soft); color: var(--ink);
  border-radius: 7px; padding: 6px 12px; cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.ghost { background: transparent; }
button:disabled { opacity: .45; cursor: default; }
.icon { padding: 2px 7px; font-size: 12px; line-height: 1.4; }

.drop { display: block; border: 2px dashed var(--line); border-radius: 10px; padding: 16px; text-align: center; color: var(--muted); cursor: pointer; font-size: 13px; }
.drop.over { border-color: var(--accent); color: var(--ink); }
.drop input { display: none; }
.filebtn { cursor: pointer; color: var(--accent); font-size: 13px; }
.filebtn input { display: none; }

.clusters { display: grid; gap: 4px; }
.cl { display: grid; grid-template-columns: 26px 44px 1fr; gap: 8px; align-items: center; }
.sw { width: 26px; height: 22px; border-radius: 5px; border: 1px solid var(--line); }
.cl .pct { font-size: 12px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.cl select { max-width: none; width: 100%; }

.filaments { display: grid; gap: 4px; }
.fil { display: grid; grid-template-columns: 30px 1fr auto auto auto auto; gap: 4px; align-items: center; }
.fil input[type=color] { width: 30px; height: 26px; padding: 0; border: 1px solid var(--line); border-radius: 5px; background: none; }
.fil input[type=text] { min-width: 0; background: var(--soft); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; font: inherit; }
.fil .base { font-size: 11px; display: flex; gap: 3px; align-items: center; color: var(--muted); }

.export { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 8px; }
.plan { font-size: 13px; }
.plan table { border-collapse: collapse; width: 100%; margin-top: 4px; }
.plan td, .plan th { padding: 3px 6px; border-bottom: 1px solid var(--line); text-align: left; }
.plan th { color: var(--muted); font-weight: 500; font-size: 12px; }
.plan .dot { display: inline-block; width: 11px; height: 11px; border-radius: 3px; border: 1px solid var(--line); vertical-align: -1px; margin-right: 5px; }
.warnings { list-style: none; padding: 0; margin: 8px 0; }
.warnings li { background: var(--warn-bg); color: var(--warn-ink); border-radius: 7px; padding: 7px 10px; margin: 4px 0; font-size: 13px; }

.tabs { display: flex; gap: 4px; padding: 8px 12px 0; }
.tabs button { border-radius: 7px 7px 0 0; border-bottom: none; background: transparent; }
.tabs button[aria-selected="true"] { background: var(--panel); font-weight: 600; }
.tab { display: none; flex: 1; min-height: 0; background: var(--panel); margin: 0 12px 12px; border-radius: 0 8px 8px 8px; border: 1px solid var(--line); overflow: hidden; }
.tab.active { display: flex; }
.svgwrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; background: var(--canvas); background-image: linear-gradient(45deg, rgba(128,128,128,.07) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.07) 75%), linear-gradient(45deg, rgba(128,128,128,.07) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.07) 75%); background-size: 20px 20px; background-position: 0 0, 10px 10px; }
.svgwrap svg { display: block; }
.empty { color: var(--muted); }
.view3d { flex: 1; min-height: 300px; }
.view3d canvas { display: block; width: 100%; height: 100%; }
.paint { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.paintbar { padding: 10px 12px 6px; border-bottom: 1px solid var(--line); }
.paintbar .hint { margin: 6px 0 0; }
.palette { display: flex; flex-wrap: wrap; gap: 6px; }
.pal { display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; }
.pal .sw { width: 16px; height: 16px; border-radius: 4px; }
.pal.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; font-weight: 600; }
.cols { flex: 1; display: grid; grid-template-columns: 3fr 1fr; gap: 12px; padding: 12px; background: var(--canvas); min-height: 0; }
.cols figure { margin: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; min-width: 0; }
.cols canvas { display: block; max-width: 100%; max-height: 100%; width: auto; height: auto; }
.vp { position: relative; overflow: hidden; touch-action: none; }
.vp.panning, .vp.panning canvas { cursor: grab !important; }
.main.vp { align-self: stretch; justify-self: stretch; border-radius: 6px; }
.main canvas { cursor: crosshair; image-rendering: pixelated; background: repeating-conic-gradient(rgba(128,128,128,.18) 0 25%, transparent 0 50%) 0 0/16px 16px; }
.main canvas[data-mode="brush"] { cursor: none; }
#t2d { position: relative; }
#svg2d { cursor: grab; }
#svg2d .empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; margin: 0; }
.zoombar { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 2px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 2px; z-index: 2; }
.zoombar button { border: none; background: transparent; padding: 3px 10px; font-size: 16px; line-height: 1.2; }
.zoombar button:hover { background: var(--soft); }
.paintrow { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; background: var(--soft); padding: 5px 10px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.brush[hidden] { display: none; }
.brush { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.brush input { width: 110px; accent-color: var(--accent); }
.brush output { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.paintact { display: inline-flex; gap: 6px; align-items: center; }
.cols .src canvas { max-height: calc(100% - 22px); opacity: .9; }
.cols figcaption { font-size: 12px; color: var(--muted); padding-top: 4px; }
.dims { position: absolute; right: 24px; bottom: 20px; font-size: 12px; color: var(--muted); background: var(--panel); padding: 3px 8px; border-radius: 6px; border: 1px solid var(--line); }
.dims:empty { display: none; }

@media (max-width: 820px) {
  html, body { height: auto; }
  .app { display: flex; flex-direction: column; }
  .panel { border-right: none; order: 2; padding: 8px 16px 24px; overflow: visible; }
  .view { order: 1; height: 46vh; position: sticky; top: 0; z-index: 1; background: var(--bg); }
  .top { padding: 10px 16px; }
  .cols { grid-template-columns: 1fr; }
  .cols .src { display: none; }
  .view:has(#tcol.active) { height: 88vh; position: static; }
  .paintbar .hint { display: none; }
}
