:root {
  --bg: #0f1013; --bg2: #16181d; --panel: #1b1e24; --line: #2a2e37; --text: #e9e6df; --muted: #9a9ca6;
  --gold: #d9b45a; --gold2: #f1d58c; --pass: #3fbf78; --warn: #f2a626; --fail: #ef4b4b; --info: #6aa8ff;
  --r: 10px; --font: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
button, select, input, textarea { font: inherit; color: inherit; }
a { color: var(--gold2); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* header */
.top { display: flex; align-items: center; gap: 16px; padding: 8px 16px; border-bottom: 1px solid var(--line); background: linear-gradient(180deg, #15171c, #111216); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--gold); white-space: nowrap; }
.brand svg { width: 30px; height: 30px; }
.brand b { font-family: 'Cormorant Garamond', serif; font-size: 22px; letter-spacing: .5px; color: var(--gold2); }
.brand span { color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs button { background: none; border: 0; padding: 8px 12px; border-radius: 8px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.tabs button.on { color: var(--text); background: var(--panel); box-shadow: inset 0 -2px 0 var(--gold); }
.top-actions { display: flex; gap: 6px; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; cursor: pointer; color: var(--text); text-decoration: none; font-size: 13px; }

/* layout */
.layout { display: grid; grid-template-columns: 340px 1fr 320px; height: calc(100vh - 51px); }
.panel { overflow-y: auto; padding: 14px; background: var(--bg2); border-right: 1px solid var(--line); }
.panel.right { border-right: 0; border-left: 1px solid var(--line); }
.panel section { display: none; }
.panel section.on { display: block; }
h3 { margin: 4px 0 8px; font-size: 16px; color: var(--gold2); }
h4 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

label { display: block; font-size: 12px; color: var(--muted); margin: 6px 0; }
select, input[type=number], input[type=text], input[type=password], textarea {
  width: 100%; margin-top: 3px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; color: var(--text);
}
textarea { resize: vertical; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btnrow { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.btnrow > * { flex: 1 1 auto; }
button { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; cursor: pointer; }
button:hover { border-color: #444a56; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: linear-gradient(180deg, #e2c069, #c79f3f); color: #1a1406; border-color: #c79f3f; font-weight: 600; }
.file { position: relative; display: inline-flex; align-items: center; justify-content: center; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; cursor: pointer; color: var(--text); font-size: 14px; margin: 0; }
.file.small { padding: 6px 8px; font-size: 12px; }
.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.drop { position: relative; border: 1.5px dashed #3a3f4a; border-radius: var(--r); padding: 16px; text-align: center; color: var(--muted); margin: 10px 0; cursor: pointer; font-size: 13px; }
.drop.over { border-color: var(--gold); color: var(--text); }
.drop input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.style-note { font-size: 12px; color: var(--muted); border-left: 2px solid var(--gold); padding-left: 8px; margin: 4px 0 10px; }

/* param form */
fieldset { border: 1px solid var(--line); border-radius: var(--r); margin: 10px 0; padding: 6px 10px 8px; }
legend { padding: 0 6px; color: var(--gold2); font-size: 12px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; cursor: pointer; user-select: none; }
fieldset.collapsed > :not(legend) { display: none; }
.f { display: grid; grid-template-columns: 1fr 74px; gap: 2px 8px; align-items: center; margin: 7px 0; }
.f > span { font-size: 12px; color: var(--muted); grid-column: 1 / -1; display: flex; justify-content: space-between; }
.f input[type=range] { width: 100%; accent-color: var(--gold); }
.f input[type=number] { margin: 0; padding: 4px 6px; }
.f.sel, .f.chk { grid-template-columns: 1fr; }
.f.chk label { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 13px; margin: 0; }
.f.chk input { accent-color: var(--gold); width: 16px; height: 16px; }
label.chk { display: flex; gap: 8px; align-items: center; color: var(--text); }

/* viewport */
.viewport { position: relative; overflow: hidden; background: radial-gradient(ellipse at 50% 40%, #23262d 0%, #121317 70%); }
#c3d { width: 100%; height: 100%; display: block; touch-action: none; }
#refOverlay { position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%; pointer-events: none; opacity: .4; display: none; }
.vp-tools { position: absolute; top: 10px; left: 10px; display: flex; gap: 4px; align-items: center; flex-wrap: wrap; background: rgba(20,22,27,.8); backdrop-filter: blur(6px); border: 1px solid var(--line); border-radius: 10px; padding: 4px; max-width: calc(100% - 20px); }
.vp-tools button { padding: 5px 9px; min-width: 32px; border-radius: 7px; font-size: 13px; }
.vp-tools button.on { border-color: var(--gold); color: var(--gold2); }
.vp-tools .sep { width: 1px; height: 20px; background: var(--line); margin: 0 3px; }
.vp-tools input[type=range] { width: 70px; accent-color: var(--gold); }
.hud { position: absolute; left: 12px; bottom: 64px; font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace; color: #cfd2d8; background: rgba(15,16,19,.7); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; pointer-events: none; }
.busy { position: absolute; right: 12px; top: 12px; padding: 6px 12px; border-radius: 20px; background: rgba(217,180,90,.15); color: var(--gold2); border: 1px solid rgba(217,180,90,.4); font-size: 12px; opacity: 0; transition: opacity .2s; pointer-events: none; }
.busy.on { opacity: 1; }

/* right cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 12px; margin-bottom: 12px; }
.card-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 8px; }
.weight .big { font-size: 40px; font-weight: 700; color: var(--gold2); line-height: 1; }
.weight .big small { font-size: 18px; color: var(--muted); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin-top: 10px; font-size: 12.5px; }
.kv b { color: var(--muted); font-weight: 500; }
.kv span { text-align: right; font-variant-numeric: tabular-nums; }
.score { font-weight: 700; padding: 2px 10px; border-radius: 20px; background: #232730; }
.checks { list-style: none; margin: 0; padding: 0; }
.checks li { padding: 7px 0 7px 22px; border-top: 1px solid var(--line); position: relative; font-size: 12.5px; }
.checks li::before { content: ''; position: absolute; left: 4px; top: 12px; width: 9px; height: 9px; border-radius: 50%; background: var(--info); }
.checks li.pass::before { background: var(--pass); } .checks li.warn::before { background: var(--warn); } .checks li.fail::before { background: var(--fail); }
.checks li b { display: block; font-size: 13px; }
.checks li .fix { color: var(--gold2); margin-top: 2px; }
.parts { list-style: none; margin: 0; padding: 0; font-size: 12.5px; }
.parts li { display: flex; gap: 8px; align-items: center; padding: 4px 0; border-top: 1px solid var(--line); }
.parts li .eye { cursor: pointer; width: 22px; text-align: center; opacity: .9; user-select: none; }
.parts li.off { opacity: .45; }
.parts li .nm { flex: 1; } .parts li .ly { color: var(--muted); font-size: 11px; }
.parts .sw { width: 10px; height: 10px; border-radius: 3px; }
.alloys { width: 100%; font-size: 12.5px; border-collapse: collapse; }
.alloys td { padding: 3px 0; border-top: 1px solid var(--line); }
.alloys td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.alloys tr.cur td { color: var(--gold2); font-weight: 600; }

/* AI / mesh / report */
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumbs figure { position: relative; margin: 0; }
.thumbs img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.thumbs button { position: absolute; top: -6px; right: -6px; padding: 0 5px; font-size: 11px; border-radius: 50%; }
.ai-out, .workflow, .report-view, #meshPlan { font-size: 13px; }
.ai-out .msg { border: 1px solid var(--line); border-radius: var(--r); padding: 10px; margin: 10px 0; background: var(--panel); overflow-x: auto; }
.ai-out .msg.user { border-color: #3a3424; }
.md h1, .md h2, .md h3 { font-size: 14px; color: var(--gold2); margin: 12px 0 6px; }
.md pre { background: #0c0d10; border: 1px solid var(--line); border-radius: 8px; padding: 8px; overflow-x: auto; font-size: 12px; }
.md code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.md table { border-collapse: collapse; width: 100%; font-size: 12px; }
.md td, .md th { border: 1px solid var(--line); padding: 3px 6px; text-align: left; }
.md ul, .md ol { padding-left: 20px; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; font-size: 11.5px; color: var(--muted); margin: 6px 0 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.workflow ol { padding-left: 18px; } .workflow li { margin: 6px 0; }
.workflow code, .report-view code { background: #0c0d10; padding: 1px 5px; border-radius: 4px; color: var(--gold2); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #22252c; border: 1px solid var(--gold); color: var(--text); border-radius: 10px; padding: 10px 16px; opacity: 0; transition: all .25s; pointer-events: none; z-index: 20; max-width: 90vw; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }
dialog { background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 14px; width: min(460px, 92vw); padding: 18px; }
dialog::backdrop { background: rgba(0,0,0,.6); }

/* v1.4: gems, library, summary */
.gem-list { list-style: none; padding: 0; margin: 0; font-size: 12px; max-height: 220px; overflow: auto; }
.gem-list li { display: flex; justify-content: space-between; gap: 6px; padding: 4px 0; border-top: 1px solid var(--line); }
.gem-list button { padding: 0 7px; }
.legend-line i, .gem-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin: 0 4px 0 6px; vertical-align: -1px; }
.gem-legend { position: absolute; right: 12px; bottom: 64px; background: rgba(255,255,255,.95); color: #111; border-radius: 10px; padding: 8px 10px; font-size: 12px; z-index: 3; max-height: 50%; overflow: auto; }
.gem-legend table { border-collapse: collapse; } .gem-legend td { padding: 2px 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; } .chips button { padding: 4px 9px; font-size: 12px; border-radius: 20px; } .chips button.on { border-color: var(--gold); color: var(--gold2); }
.lib-grid { display: grid; grid-template-columns: 1fr; gap: 6px; }
.asset { display: grid; grid-template-columns: 52px 1fr auto; gap: 8px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; }
.asset .thumb { width: 52px; height: 52px; border-radius: 8px; background: #121317; display: grid; place-items: center; overflow: hidden; font-size: 22px; }
.asset .thumb img { width: 100%; height: 100%; object-fit: cover; }
.asset .meta { min-width: 0; display: flex; flex-direction: column; } .asset .meta b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.asset .acts { display: flex; gap: 4px; } .asset .acts button { padding: 4px 8px; font-size: 12px; }
.custom-alloy { margin-top: 8px; } .custom-alloy input { margin: 0; padding: 5px 7px; font-size: 12px; }
.summary h2 { margin: 0; color: var(--gold2); font-size: 18px; } .summary h3 { font-size: 14px; color: var(--gold2); margin: 14px 0 6px; }
.sum-head { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.sum-shot { width: 120px; border-radius: 8px; } .sum-map { width: 100%; border-radius: 8px; background: #fff; }
.sum-kv, .sum-t { width: 100%; border-collapse: collapse; font-size: 12px; }
.sum-kv th { text-align: left; color: var(--muted); font-weight: 500; padding: 3px 8px 3px 0; white-space: nowrap; } .sum-kv td, .sum-t td, .sum-t th { padding: 3px 6px; border-top: 1px solid var(--line); text-align: left; }
.sum-t tr.cur td { color: var(--gold2); font-weight: 600; } .sum-t tr.tot td { font-weight: 700; }
.sum-checks { padding-left: 18px; font-size: 12px; } .sum-checks li.fail b { color: var(--fail); } .sum-checks li.warn b { color: var(--warn); }

/* v1.3 */
details.tool { border: 1px solid var(--line); border-radius: var(--r); padding: 6px 10px; margin: 8px 0; background: var(--panel); }
details.tool summary { cursor: pointer; color: var(--gold2); font-size: 13px; }
details.tool textarea { margin: 8px 0; }
#sketchPad { width: 100%; aspect-ratio: 1; background: #fff; border-radius: 8px; margin-top: 8px; touch-action: none; cursor: crosshair; }
.seg.brush { top: auto; bottom: 6px; right: 6px; align-items: center; }
.seg.brush input { width: 46px; padding: 2px 4px; margin: 0; font-size: 11px; }
button.big { width: 100%; padding: 12px; font-size: 14px; margin-bottom: 6px; }
.small-btn { padding: 3px 9px; font-size: 12px; border-radius: 20px; border-color: var(--gold); color: var(--gold2); }
fieldset.mixer { margin-top: 4px; }

/* edit mode + AI bar */
.edit-panel { position: absolute; right: 12px; top: 56px; width: 220px; background: rgba(20,22,27,.94); border: 1px solid var(--gold); border-radius: 12px; padding: 10px; font-size: 12.5px; z-index: 3; }
.edit-panel b { display: block; color: var(--gold2); margin-bottom: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-panel .seg { display: flex; gap: 4px; margin-bottom: 6px; }
.edit-panel .seg button { flex: 1; padding: 5px; font-size: 12px; }
.edit-panel input[type=number] { margin-top: 2px; }
.edit-panel .btnrow { margin: 6px 0 0; }
.edit-panel .btnrow button { font-size: 12px; padding: 6px; }
.ai-bar { position: absolute; left: 50%; transform: translateX(-50%); bottom: 14px; width: min(640px, calc(100% - 300px)); display: flex; gap: 6px; align-items: center; background: rgba(20,22,27,.92); border: 1px solid #3a3424; border-radius: 30px; padding: 5px 5px 5px 14px; z-index: 2; backdrop-filter: blur(6px); }
.ai-bar input { flex: 1; background: none; border: 0; margin: 0; padding: 6px 2px; outline: none; color: var(--text); }
.ai-bar button { border-radius: 24px; padding: 7px 14px; }
@media (max-width: 760px) { .ai-bar { width: calc(100% - 24px); bottom: 10px; } .hud { bottom: 64px; } .edit-panel { top: auto; bottom: 64px; right: 8px; } }

/* image → 3D */
.img3d-preview { position: relative; margin: 8px 0; border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; background: repeating-conic-gradient(#1d2026 0% 25%, #23262d 0% 50%) 50% / 16px 16px; }
.img3d-preview canvas { display: block; width: 100%; height: auto; }
.img3d-preview .seg { position: absolute; top: 6px; right: 6px; display: flex; gap: 2px; }
.seg button { padding: 3px 8px; font-size: 11px; border-radius: 6px; background: rgba(20,22,27,.85); }
.seg button.on { border-color: var(--gold); color: var(--gold2); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.grid2 button { font-size: 12.5px; }
#img3dInfo { margin-top: 10px; }
.f input[type=text] { grid-column: 1 / -1; margin: 0; }
dialog.wide { width: min(920px, 96vw); }
.var-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.var-grid figure { margin: 0; cursor: pointer; border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; background: #121317; }
.var-grid figure:hover { border-color: var(--gold); }
.var-grid img { width: 100%; display: block; aspect-ratio: 1; object-fit: cover; }
.var-grid figcaption { font-size: 11.5px; padding: 6px 8px; color: var(--muted); }
@media (max-width: 600px) { .var-grid { grid-template-columns: repeat(2, 1fr); } .grid2 { grid-template-columns: 1fr; } }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 320px 1fr; grid-template-rows: 1fr auto; height: auto; min-height: calc(100vh - 51px); }
  .panel.right { grid-column: 1 / -1; border-left: 0; border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; }
  .panel.right .card { margin: 0; }
  .viewport { min-height: 70vh; }
  .panel.left { max-height: calc(100vh - 51px); }
}
@media (max-width: 760px) {
  .top { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .brand span { display: none; }
  .tabs { order: 3; flex-basis: 100%; }
  .layout { grid-template-columns: 1fr; }
  .viewport { order: -1; min-height: 52vh; height: 52vh; }
  .panel.left { max-height: none; border-right: 0; }
  .panel { padding: 12px 16px; }
}
@media print {
  body { background: #fff; color: #111; }
  .top, .viewport, .panel.right, .panel.left > section:not([data-panel=report]), .panel.left h3 + p, .btnrow, .row2 { display: none !important; }
  .layout { display: block; height: auto; }
  .panel { overflow: visible; background: #fff; border: 0; }
  .report-view { color: #111; }
  .md h1, .md h2, .md h3 { color: #6b4f12; }
  .md td, .md th { border-color: #bbb; }
}
