:root {
  --navy: #14213d;
  --navy-2: #1d2f55;
  --ink: #1b2333;
  --muted: #667085;
  --line: #dde3ec;
  --panel: #ffffff;
  --bg: #f3f5f9;
  --accent: #2f7cf6;
  --accent-soft: #e6efff;
  --green: #2e9e6a;
  --amber: #e69500;
  --red: #d64545;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { display: grid; grid-template-rows: 52px 1fr 44px; background: var(--bg); overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
kbd { font: 11px ui-monospace, Menlo, Consolas, monospace; background: #eef1f6; border: 1px solid #d3d9e3; border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; color: #475467; }

/* Top bar */
.topbar { display: flex; align-items: center; gap: 16px; padding: 0 12px; background: var(--navy); color: #fff; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 30px; height: 30px; }
.brand-text { line-height: 1.05; white-space: nowrap; }
.brand-text strong { font-size: 16px; letter-spacing: .2px; }
.brand-text span { font-size: 14px; opacity: .85; }
.brand-text small { display: block; font-size: 11px; opacity: .65; }
.title-wrap { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; min-width: 0; }
.track-title { width: min(440px, 100%); background: transparent; border: 1px solid transparent; color: #fff; font-size: 17px; font-weight: 600; text-align: center; padding: 6px 10px; border-radius: 8px; }
.track-title:hover { border-color: rgba(255,255,255,.2); }
.track-title:focus { outline: none; background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.4); }
.dirty-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); visibility: hidden; }
.dirty-dot.on { visibility: visible; }
.actions { display: flex; align-items: center; gap: 6px; }
.btn { background: rgba(255,255,255,.08); border: 1px solid rgba(255,255,255,.16); color: #fff; padding: 6px 12px; border-radius: 8px; font-size: 13px; }
.btn:hover { background: rgba(255,255,255,.16); }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.icon-btn { background: transparent; border: 1px solid transparent; color: inherit; width: 32px; height: 32px; border-radius: 8px; font-size: 16px; display: inline-grid; place-items: center; }
.icon-btn:hover { background: rgba(127,127,127,.15); }
.addon-btn { display: inline-flex; align-items: center; gap: 6px; }
.pill { font-size: 10px; text-transform: uppercase; letter-spacing: .5px; background: var(--amber); color: #1b1300; border-radius: 99px; padding: 1px 6px; font-weight: 700; }
.menu { position: relative; }
.menu-list { position: absolute; right: 0; top: calc(100% + 6px); background: #fff; color: var(--ink); border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.18); padding: 6px; min-width: 230px; z-index: 30; }
.menu-list button { display: flex; justify-content: space-between; width: 100%; text-align: left; padding: 8px 10px; background: none; border: 0; border-radius: 6px; font-size: 13px; }
.menu-list button:hover { background: var(--accent-soft); }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

/* Layout */
.layout { display: grid; grid-template-columns: 250px 1fr 320px; min-height: 0; }
.palette, .side { background: var(--panel); min-height: 0; overflow: auto; }
.palette { border-right: 1px solid var(--line); padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.side { border-left: 1px solid var(--line); display: flex; flex-direction: column; }
.palette-controls { display: grid; gap: 8px; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: #eef1f6; border-radius: 8px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 4px; border-radius: 6px; font-size: 12.5px; color: #475467; }
.seg button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); font-weight: 600; }
#partSearch { width: 100%; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; font-size: 13px; }
.palette-groups { display: grid; gap: 6px; }
.pgroup > summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); padding: 6px 2px; }
.pgroup > summary::-webkit-details-marker { display: none; }
.pgroup > summary::before { content: '▸'; margin-right: 6px; transition: transform .15s; }
.pgroup[open] > summary::before { transform: rotate(90deg); }
.pgroup > summary .grp-label { flex: 1; }
.pgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pcard { position: relative; border: 1px solid var(--line); border-left: 4px solid var(--fam, var(--line)); background: #fbfcfe; border-radius: 9px; padding: 6px 6px 5px; text-align: left; touch-action: none; user-select: none; transition: border-color .1s, box-shadow .1s, transform .08s; }
.pcard:hover { border-color: #b8c6dc; box-shadow: var(--shadow); }
.pcard:active { transform: scale(.98); }
.pcard.on { border-color: var(--fam, var(--accent)); background: var(--fam-soft, var(--accent-soft)); box-shadow: 0 0 0 2px var(--fam, var(--accent)); }
.pbody { display: grid; gap: 4px; }
.psub { display: flex; align-items: center; gap: 7px; font-size: 12px; font-weight: 600; color: #344054; margin: 6px 0 2px; }
.psub i { width: 10px; height: 10px; border-radius: 3px; background: var(--fam); flex: none; }
.psub small { margin-left: auto; font-weight: 500; color: var(--muted); font-size: 10.5px; }
.pcard canvas { width: 100%; height: 52px; display: block; }
.pcard .pname { font-size: 11.5px; font-weight: 600; line-height: 1.2; margin-top: 3px; }
.pcard .pmeta { font-size: 10.5px; color: var(--muted); }
.pcard .pdef { position: absolute; top: 4px; right: 5px; font-size: 8.5px; font-weight: 700; color: #fff; background: var(--fam, var(--green)); border-radius: 4px; padding: 0 4px; }
.palette-tip { font-size: 11.5px; color: var(--muted); margin: 4px 0 0; line-height: 1.4; }

/* Stage */
.stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#canvas { width: 100%; height: 100%; display: block; outline: none; touch-action: none; }
.toolbar { position: absolute; top: 10px; left: 10px; display: flex; align-items: center; gap: 2px; background: #fff; border-radius: 10px; box-shadow: var(--shadow); padding: 4px; z-index: 5; }
.tool { min-width: 32px; height: 32px; border: 0; background: transparent; border-radius: 7px; font-size: 15px; display: inline-flex; align-items: center; justify-content: center; gap: 5px; padding: 0 7px; color: #344054; }
.tool span { font-size: 12.5px; }
.tool:hover { background: #f0f3f8; }
.tool.on { background: var(--accent-soft); color: var(--accent); }
.tool:disabled { opacity: .35; cursor: default; }
.tool.is-disabled { opacity: .38; cursor: not-allowed; }
.tool.busy { color: var(--accent); }
.tool.busy span::after { content: '…'; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .2; } }
.tool.zoom-label { font-size: 12px; min-width: 52px; font-variant-numeric: tabular-nums; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 3px; }
.hud { position: absolute; left: 10px; bottom: 10px; z-index: 5; background: rgba(20,33,61,.88); color: #fff; font-size: 12px; padding: 7px 10px; border-radius: 8px; pointer-events: none; max-width: 70%; line-height: 1.5; }
.hud b { color: #9cc3ff; }
.hud kbd { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.25); color: #fff; }

/* Side panel */
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; background: none; border: 0; padding: 12px 6px; font-size: 13px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.tab-body { padding: 12px; overflow: auto; }
.bom-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.bom-table th { text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); font-weight: 700; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.bom-table td { padding: 7px 4px; border-bottom: 1px solid #eef1f6; vertical-align: top; }
.bom-table td.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bom-table tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: 0; }
.bom-table .sub { display: block; font-size: 11px; color: var(--muted); }
.bom-table a { color: var(--accent); text-decoration: none; }
.qty { display: inline-block; min-width: 26px; text-align: center; background: var(--navy); color: #fff; border-radius: 6px; font-weight: 700; padding: 1px 4px; }
.note { font-size: 11.5px; color: var(--muted); line-height: 1.45; }
.empty { text-align: center; color: var(--muted); font-size: 13px; padding: 30px 10px; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; font-size: 13px; align-items: center; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.row-btns { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.sbtn { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12.5px; }
.sbtn:hover { border-color: #b8c6dc; background: #f8fafc; }
.sbtn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.sbtn.danger { color: var(--red); }

/* Status bar */
.statusbar { display: flex; align-items: center; gap: 4px; padding: 0 10px; background: #fff; border-top: 1px solid var(--line); font-size: 12.5px; overflow: hidden; }
.stat { display: flex; align-items: baseline; gap: 7px; padding: 0 12px; border-right: 1px solid var(--line); white-space: nowrap; }
.stat span { color: var(--muted); }
.stat strong { font-variant-numeric: tabular-nums; font-size: 14px; }
.stat-flags { gap: 6px; border-right: 0; }
.flag { font-size: 11.5px; padding: 2px 8px; border-radius: 99px; background: #eef1f6; color: #475467; }
.flag.ok { background: #e3f5ec; color: #1d7a4f; }
.flag.warn { background: #fff3db; color: #8a5a00; }
.flag.bad { background: #fde7e7; color: #a32727; }
.stat-right { margin-left: auto; border-right: 0; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: #fff; border-radius: 14px; width: min(860px, 100%); max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 24px 64px rgba(0,0,0,.3); }
.modal.small { width: min(440px, 100%); }
.modal > header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal h2 { font-size: 16px; margin: 0; }
.modal-body { padding: 16px 18px; overflow: auto; font-size: 13.5px; line-height: 1.5; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
.modal-foot:empty { display: none; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 16px; }
.field { display: grid; gap: 4px; font-size: 12.5px; }
.field label { font-weight: 600; }
.field small { color: var(--muted); font-size: 11px; }
.field input, .field select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; font: inherit; font-size: 13px; }
.field.check { grid-template-columns: auto 1fr; align-items: center; }
.settings-tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; }
.settings-tabs button { border: 1px solid var(--line); background: #fff; border-radius: 99px; padding: 5px 12px; font-size: 12.5px; }
.settings-tabs button.on { background: var(--navy); color: #fff; border-color: var(--navy); }
.parts-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.parts-table th, .parts-table td { padding: 4px; border-bottom: 1px solid #eef1f6; text-align: left; }
.parts-table input[type=number] { width: 70px; padding: 3px 5px; border: 1px solid var(--line); border-radius: 5px; font: inherit; }
.parts-table input[type=text] { width: 150px; padding: 3px 5px; border: 1px solid var(--line); border-radius: 5px; font: inherit; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.lib-card { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; display: flex; flex-direction: column; }
.lib-card img { width: 100%; aspect-ratio: 4/3; object-fit: cover; background: #0f4c81; display: block; }
.lib-card .meta { padding: 8px 10px; font-size: 12px; color: var(--muted); }
.lib-card .meta strong { display: block; color: var(--ink); font-size: 13.5px; margin-bottom: 2px; }
.lib-card .row-btns { padding: 0 10px 10px; margin-top: 0; }
.shortcuts { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 4px 24px; }
.shortcuts div { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; border-bottom: 1px dashed #eef1f6; font-size: 13px; }

.toast { position: fixed; left: 50%; top: 64px; transform: translateX(-50%); background: var(--navy); color: #fff; padding: 9px 16px; border-radius: 10px; font-size: 13px; z-index: 60; box-shadow: 0 10px 30px rgba(0,0,0,.25); }
.drag-ghost { position: fixed; pointer-events: none; z-index: 70; background: var(--accent); color: #fff; font-size: 12px; font-weight: 600; padding: 4px 9px; border-radius: 99px; transform: translate(12px, 12px); }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 210px 1fr 270px; }
  .brand-text span, .brand-text small { display: none; }
}
@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr 170px; }
  .palette { grid-row: 2; border: 0; border-top: 1px solid var(--line); }
  .pgrid { grid-template-columns: repeat(4, 1fr); }
  .title-wrap { display: none; }
  .side { display: none; }
  .actions .btn { padding: 6px 8px; }
}
