:root {
    --bg: #15161a;
    --panel: #1e2025;
    --panel2: #262930;
    --panel3: #2f333b;
    --line: #353a44;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --dim: #6b7280;
    --accent: #ff7a1a;
    --accent-2: #ffb070;
    --blue: #3b8cff;
    --danger: #ef4444;
    --ok: #22c55e;
    --warn: #f59e0b;
    --radius: 7px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.35 var(--font); overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
input[type=number] { -moz-appearance: textfield; }
input[type=number]::-webkit-inner-spin-button { opacity: .5; }
::selection { background: var(--accent); color: #000; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--panel3); border-radius: 10px; border: 2px solid var(--panel); }

#app { display: grid; grid-template-rows: 44px auto 1fr 26px; height: 100vh; }

/* ---------- barra de menus ---------- */
#menubar { display: flex; align-items: center; gap: 6px; padding: 0 10px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 7px; font-size: 16px; letter-spacing: .3px; margin-right: 6px; user-select: none; }
.brand b { color: var(--accent); }
#menus { display: flex; gap: 1px; }
.menu { position: relative; }
.menu > button { background: none; border: 0; padding: 7px 10px; border-radius: 6px; cursor: pointer; color: var(--text); }
.menu > button:hover, .menu.open > button { background: var(--panel3); }
.menu .drop { display: none; position: absolute; top: 100%; left: 0; min-width: 260px; background: var(--panel2); border: 1px solid var(--line); border-radius: 9px; padding: 5px; z-index: 50; box-shadow: 0 12px 34px rgba(0,0,0,.45); }
.menu.open .drop { display: block; }
.drop button { display: flex; width: 100%; align-items: center; gap: 10px; background: none; border: 0; text-align: left; padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.drop button:hover { background: var(--accent); color: #111; }
.drop button:hover kbd { color: #111; }
.drop button[disabled] { opacity: .4; pointer-events: none; }
.drop hr { border: 0; border-top: 1px solid var(--line); margin: 5px 4px; }
.drop kbd { margin-left: auto; color: var(--muted); font: 11px/1 var(--font); }
.drop .ico { width: 16px; height: 16px; flex: none; opacity: .9; }
#docname { width: 200px; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 5px 8px; margin-left: 10px; color: var(--muted); }
#docname:hover, #docname:focus { border-color: var(--line); background: var(--bg); color: var(--text); outline: none; }
.spacer { flex: 1; }

.btn { border: 1px solid var(--line); background: var(--panel2); padding: 6px 12px; border-radius: var(--radius); cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--panel3); }
.btn.accent { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }
.btn.accent:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; }
#btn-save { min-width: 96px; }
#btn-save[data-state="saved"] { color: var(--ok); border-color: transparent; }
#btn-save[data-state="saving"], #btn-save[data-state="dirty"] { color: var(--muted); }
#btn-save[data-state="error"], #btn-save[data-state="conflict"] { color: #fff; background: #b42323; border-color: #b42323; }
.btn.danger { color: #fff; background: #b42323; border-color: #b42323; }
.btn.sm { padding: 4px 8px; font-size: 12px; }
.btn.active { background: var(--accent); color: #111; border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: default; }

/* ---------- controles da ferramenta ---------- */
#toolctrl { display: flex; align-items: center; gap: 12px; padding: 6px 12px; min-height: 38px; background: var(--panel2); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
#toolctrl .tname { font-weight: 600; color: var(--accent-2); margin-right: 4px; }
#toolctrl label { display: flex; align-items: center; gap: 5px; color: var(--muted); }
#toolctrl input[type=number] { width: 62px; }
#toolctrl .sep { width: 1px; align-self: stretch; background: var(--line); }

/* ---------- campos ---------- */
.field, input[type=text].field, select.field, textarea.field, input.num {
    background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; outline: none; min-width: 0;
}
.field:focus, input.num:focus { border-color: var(--accent); }
input.num { width: 100%; text-align: right; }
select.field { padding: 4px; }
textarea.field { width: 100%; resize: vertical; min-height: 54px; }
input[type=checkbox] { accent-color: var(--accent); }
input[type=range] { accent-color: var(--accent); }
input[type=color] { border: 1px solid var(--line); background: var(--bg); width: 34px; height: 26px; padding: 1px; border-radius: 6px; cursor: pointer; }

/* ---------- área principal ---------- */
#main { display: grid; grid-template-columns: 56px 1fr 300px; min-height: 0; }
#toolbox { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 6px 0; gap: 3px; overflow-y: auto; overflow-x: hidden; }
#toolbox .tb { width: 40px; height: 38px; display: grid; place-items: center; border-radius: 8px; border: 1px solid transparent; background: none; cursor: pointer; color: var(--text); position: relative; flex: none; }
#toolbox .tb:hover { background: var(--panel3); }
#toolbox .tb.on { background: var(--accent); color: #111; }
#toolbox .tb svg { width: 22px; height: 22px; }
#toolbox .tsep { width: 30px; height: 1px; background: var(--line); margin: 4px 0; flex: none; }
#toolbox .tlabel { font-size: 9px; color: var(--dim); text-transform: uppercase; letter-spacing: .5px; margin-top: 2px; flex: none; }

#stage { position: relative; overflow: hidden; background: radial-gradient(ellipse at 50% 30%, #23262d, #131418 75%); min-width: 0; }
#stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
#viewbar { position: absolute; top: 10px; right: 10px; display: flex; gap: 4px; background: rgba(30,32,37,.86); border: 1px solid var(--line); border-radius: 10px; padding: 4px; backdrop-filter: blur(6px); }
#viewbar button { background: none; border: 0; color: var(--text); padding: 5px 8px; border-radius: 7px; cursor: pointer; font-size: 12px; }
#viewbar button:hover { background: var(--panel3); }
#viewbar button.on { background: var(--accent); color: #111; font-weight: 600; }
#viewbar .vsep { width: 1px; background: var(--line); margin: 2px 2px; }
#hint { position: absolute; left: 12px; bottom: 10px; max-width: calc(100% - 24px); color: var(--muted); background: rgba(21,22,26,.75); padding: 6px 10px; border-radius: 8px; font-size: 12px; pointer-events: none; }
#hint:empty { display: none; }
#hint b { color: var(--text); }

/* overlay SVG */
#overlay .frame { fill: none; stroke: #ffb070; stroke-width: 1.2; stroke-dasharray: 5 4; }
#overlay .frame.bad { stroke: var(--danger); stroke-dasharray: none; stroke-width: 2; }
#overlay .hd { fill: #fff; stroke: #c4520a; stroke-width: 1.5; }
#overlay .hd.edge { fill: #ffd2ae; }
#overlay .hd.hz { fill: #3b8cff; stroke: #fff; }
#overlay .hd.lift { fill: #111; stroke: #fff; }
#overlay .hd.rot { fill: #22c55e; stroke: #fff; }
#overlay .rotico { fill: none; stroke: #fff; stroke-width: 1.6; }
#overlay .dim { fill: #fff; font: 600 11px var(--font); paint-order: stroke; stroke: rgba(0,0,0,.75); stroke-width: 3px; }
#overlay .dim.big { font-size: 13px; }
#overlay .dimbox { fill: rgba(21,22,26,.88); stroke: #ffb070; stroke-width: 1; }
#overlay .band { fill: rgba(59,140,255,.12); stroke: #3b8cff; stroke-width: 1; stroke-dasharray: 4 3; }
#overlay .draw { fill: rgba(255,122,26,.16); stroke: #ff7a1a; stroke-width: 1.6; }
#overlay .draw.open { fill: none; }
#overlay .drawline { fill: none; stroke: #ff7a1a; stroke-width: 1.6; }
#overlay .preview { fill: none; stroke: #ffb070; stroke-width: 1.2; stroke-dasharray: 4 3; }
#overlay .pathline { fill: none; stroke: #3b8cff; stroke-width: 1.6; }
#overlay .node { fill: #fff; stroke: #1d4ed8; stroke-width: 1.4; }
#overlay .node.sel { fill: #3b8cff; stroke: #fff; }
#overlay .node.first { fill: #22c55e; }
#overlay .hline { stroke: #93c5fd; stroke-width: 1; }
#overlay .hdl { fill: #93c5fd; stroke: #1d4ed8; stroke-width: 1; }
#overlay .measure { stroke: #22c55e; stroke-width: 2; }
#overlay .mdot { fill: #22c55e; stroke: #fff; stroke-width: 1.5; }
#overlay .bedlbl { fill: rgba(255,255,255,.28); font: 700 11px var(--font); letter-spacing: 3px; }

/* ---------- painel direito ---------- */
#panel { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
#tabs button { flex: 1; background: none; border: 0; padding: 10px 2px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; font-size: 12px; }
#tabs button.on { color: var(--text); border-bottom-color: var(--accent); }
#tabbody { overflow-y: auto; padding: 10px 12px 30px; flex: 1; }
.sec { margin-bottom: 14px; }
.sec h4 { margin: 0 0 7px; font-size: 11px; text-transform: uppercase; letter-spacing: .7px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.sec h4 .spacer { flex: 1; }
.row { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; }
.row > label { color: var(--muted); min-width: 74px; font-size: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; margin-bottom: 6px; }
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px; margin-bottom: 6px; }
.nf { position: relative; display: flex; align-items: center; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; }
.nf:focus-within { border-color: var(--accent); }
.nf span { padding: 0 0 0 7px; color: var(--muted); font-size: 11px; cursor: ew-resize; user-select: none; white-space: nowrap; }
.nf span.x { color: #f87171; } .nf span.y { color: #4ade80; } .nf span.z { color: #60a5fa; }
.nf input { border: 0; background: none; width: 100%; padding: 4px 6px; text-align: right; outline: none; min-width: 0; }
.nf em { font-style: normal; color: var(--dim); font-size: 10px; padding-right: 6px; }
.btnrow { display: flex; flex-wrap: wrap; gap: 5px; }
.btnrow .btn { flex: 1 1 auto; }
.icobtn { display: inline-grid; place-items: center; width: 34px; height: 30px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel2); cursor: pointer; }
.icobtn:hover { background: var(--panel3); }
.icobtn svg { width: 18px; height: 18px; }
.swatches { display: flex; flex-wrap: wrap; gap: 4px; }
.swatches button { width: 20px; height: 20px; border-radius: 50%; border: 2px solid rgba(255,255,255,.15); cursor: pointer; padding: 0; }
.swatches button:hover { transform: scale(1.15); border-color: #fff; }
.swatches button.on { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
.toggle { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 2px; gap: 2px; }
.toggle button { flex: 1; border: 0; background: none; padding: 4px 6px; border-radius: 5px; cursor: pointer; color: var(--muted); font-size: 12px; }
.toggle button.on { background: var(--panel3); color: var(--text); }
.toggle button.on.hole { background: #4b5563; }
.toggle button.on.solid { background: var(--accent); color: #111; }
.muted { color: var(--muted); }
.small { font-size: 11px; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; line-height: 1.6; }
.empty .big { font-size: 34px; margin-bottom: 6px; }
.tip { background: var(--panel2); border: 1px solid var(--line); border-left: 3px solid var(--accent); padding: 8px 10px; border-radius: 6px; color: var(--muted); font-size: 12px; line-height: 1.5; }
.tip b { color: var(--text); }
.warnbox { border-left-color: var(--danger); }

/* lista de objetos */
.objlist { list-style: none; margin: 0; padding: 0; }
.objlist li { display: flex; align-items: center; gap: 7px; padding: 5px 6px; border-radius: 6px; cursor: pointer; user-select: none; }
.objlist li:hover { background: var(--panel2); }
.objlist li.sel { background: rgba(255,122,26,.18); outline: 1px solid rgba(255,122,26,.5); }
.objlist li .dot { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.objlist li .dot.hole { background: repeating-linear-gradient(45deg, #6b7280 0 2px, transparent 2px 4px) !important; border: 1px solid #9ca3af; }
.objlist li .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.objlist li .ty { color: var(--dim); font-size: 11px; }
.objlist li .ib { background: none; border: 0; cursor: pointer; opacity: .55; padding: 2px; color: var(--text); }
.objlist li .ib:hover { opacity: 1; }
.objlist li .ib.off { opacity: .25; }
.objlist ul { list-style: none; padding-left: 18px; margin: 0; }
.objlist ul li { cursor: default; color: var(--muted); font-size: 12px; padding: 3px 6px; }

.stats { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 12px; }
.stats span:nth-child(odd) { color: var(--muted); }
.stats b { font-variant-numeric: tabular-nums; }

/* ---------- barra de status ---------- */
#statusbar { display: flex; align-items: center; gap: 16px; padding: 0 12px; background: var(--panel); border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; }
#st-warn { color: var(--warn); }
#st-xy { font-variant-numeric: tabular-nums; min-width: 170px; text-align: right; }

/* ---------- modais ---------- */
#modal-root .backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 100; animation: fade .12s ease; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 60px); display: flex; flex-direction: column; box-shadow: 0 24px 60px rgba(0,0,0,.55); }
.modal.wide { width: min(860px, calc(100vw - 32px)); }
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--line); font-size: 15px; font-weight: 600; display: flex; align-items: center; }
.modal header .x { margin-left: auto; background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal .body { padding: 16px 18px; overflow-y: auto; }
.modal footer { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }
.modal .row > label { min-width: 150px; }
.choice { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.choice button { border: 2px solid var(--line); background: var(--panel2); border-radius: 10px; padding: 12px 8px; cursor: pointer; text-align: center; }
.choice button b { display: block; font-size: 16px; margin-bottom: 3px; }
.choice button span { color: var(--muted); font-size: 11px; }
.choice button.on { border-color: var(--accent); background: rgba(255,122,26,.12); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.card { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; cursor: pointer; position: relative; }
.card:hover { border-color: var(--accent); }
.card img, .card .noimg { width: 100%; aspect-ratio: 16 / 10; object-fit: cover; display: block; background: #111; }
.card .meta { padding: 8px 10px; }
.card .meta b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta span { color: var(--muted); font-size: 11px; }
.card .del { position: absolute; top: 6px; right: 6px; background: rgba(0,0,0,.6); border: 0; color: #fff; border-radius: 6px; padding: 3px 7px; cursor: pointer; opacity: 0; }
.card:hover .del { opacity: 1; }
.card .del:hover { background: var(--danger); }
.tpl { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left !important; padding: 12px !important; }
.tpl .emo { font-size: 26px; }
.keys { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 6px 14px; font-size: 12px; }
.keys kbd { background: var(--panel3); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px; font: 11px var(--font); white-space: nowrap; justify-self: start; }

/* ---------- toasts / carregando ---------- */
#toasts { position: fixed; bottom: 40px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 200; pointer-events: none; align-items: center; }
.toast { background: var(--panel3); border: 1px solid var(--line); padding: 9px 16px; border-radius: 9px; box-shadow: 0 10px 30px rgba(0,0,0,.4); animation: up .18s ease; max-width: 520px; }
.toast.ok { border-left: 3px solid var(--ok); }
.toast.err { border-left: 3px solid var(--danger); }
.toast.warn { border-left: 3px solid var(--warn); }
#loading { position: fixed; inset: 0; background: var(--bg); display: grid; place-content: center; justify-items: center; z-index: 300; color: var(--muted); }
#loading.gone { opacity: 0; pointer-events: none; transition: opacity .3s; }
.spin { width: 38px; height: 38px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: rot 0.8s linear infinite; }
#textedit { position: absolute; z-index: 20; background: var(--panel); border: 2px solid var(--accent); border-radius: 8px; padding: 6px; display: flex; gap: 6px; box-shadow: 0 10px 30px rgba(0,0,0,.5); }
#textedit input { width: 220px; }
#dimedit { position: absolute; z-index: 20; background: var(--panel); border: 2px solid var(--accent); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; gap: 6px; box-shadow: 0 10px 30px rgba(0,0,0,.5); }
#dimedit .dimrow { display: flex; align-items: center; gap: 6px; }
#dimedit .dimrow > label { color: var(--muted); font-size: 12px; white-space: nowrap; }
#dimedit input.field { width: 84px; text-align: right; font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
#dimedit .arrows { display: flex; flex-direction: column; gap: 2px; }
#dimedit .arrow { width: 26px; height: 15px; border: 1px solid var(--line); background: var(--panel2); color: var(--text); border-radius: 4px; font-size: 9px; line-height: 1; cursor: pointer; padding: 0; user-select: none; touch-action: none; }
#dimedit .arrow:hover { background: var(--accent); color: #111; border-color: var(--accent); }
#dimedit label.small { display: flex; align-items: center; gap: 5px; cursor: pointer; }

@keyframes rot { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { opacity: 0; transform: translateY(8px); } }

/* ---------- telas menores ---------- */
@media (max-width: 1000px) {
    #main { grid-template-columns: 52px 1fr 250px; }
    #docname { display: none; }
}
@media (max-width: 760px) {
    #main { grid-template-columns: 48px 1fr 210px; }
    .brand span, #btn-save, #toolctrl .opt { display: none; }
}
#loading .boot-error { color: #f87171; max-width: 520px; text-align: center; padding: 0 16px; }

/* página de erro (404) */
body.errpage { display: grid; place-items: center; min-height: 100vh; overflow: auto; }
.errbox { text-align: center; max-width: 460px; padding: 32px 16px; }
.errbox .big { font-size: 48px; }
.errbox h1 { font-size: 22px; margin: 8px 0; }
.errbox p { color: var(--muted); line-height: 1.6; margin-bottom: 22px; }
.errbox .btn { text-decoration: none; display: inline-block; }
