:root {
  --bg: #eef0f4; --panel: #ffffff; --line: #e1e4ea; --text: #1d2430; --muted: #6b7686;
  --accent: #4f46e5; --accent-weak: #eef0ff; --sel: #0d9488; --danger: #dc2626;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; background: var(--bg); font-size: 13px; }
body { display: grid; grid-template-columns: 48px 1fr 270px; grid-template-rows: auto 1fr; grid-template-areas: "top top top" "tools stage panel"; }

#topbar { grid-area: top; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 5px 10px; min-height: 36px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: -.01em; margin-right: 4px; }
.logo { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 7px; background: var(--accent); color: #fff; font-weight: 800; }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.spacer { flex: 1; }
.name { border: 1px solid transparent; background: transparent; font: inherit; font-weight: 600; width: 150px; padding: 5px 7px; border-radius: 6px; }
.name:hover, .name:focus { border-color: var(--line); outline: none; background: #fff; }
button { font: inherit; color: var(--text); background: #fff; border: 1px solid var(--line); border-radius: 7px; padding: 5px 10px; cursor: pointer; }
button:hover:not(:disabled) { background: #f5f6fa; border-color: #cfd4dd; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover:not(:disabled) { background: #4338ca; }
button.danger { color: var(--danger); }
input[type=number], select, input[type=text] { font: inherit; border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; background: #fff; color: var(--text); }
input[type=number] { width: 64px; }
.field { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); }
.field input[type=number] { width: 58px; }

#tools { grid-area: tools; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 0; }
#tools button { width: 36px; height: 36px; padding: 0; display: grid; place-items: center; border-color: transparent; }
#tools button.active { background: var(--accent-weak); border-color: var(--accent); }
#tools svg { width: 20px; height: 20px; fill: var(--text); stroke: var(--text); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
#tools svg .hollow { fill: none; }
#tools button.active svg { stroke: var(--accent); fill: var(--accent); }
#tools button.active svg .hollow { fill: none; }

#stage { grid-area: stage; position: relative; overflow: hidden; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; user-select: none; }
#canvas.t-select { cursor: default; }
#canvas.t-node { cursor: default; }
#canvas.t-rect, #canvas.t-ellipse, #canvas.t-line, #canvas.t-pen { cursor: crosshair; }
#canvas.panning { cursor: grabbing; }
#canvas.space { cursor: grab; }
#board { filter: drop-shadow(0 1px 6px rgba(20, 30, 50, .18)); }
#gridlines { stroke: #dfe3ea; stroke-width: 1; vector-effect: non-scaling-stroke; }
#boardEdge { stroke: #b8bfcc; stroke-width: 1; vector-effect: non-scaling-stroke; }
.obj { vector-effect: non-scaling-stroke; stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; pointer-events: none; }
.ov-sel { fill: none; stroke: var(--sel); stroke-width: 1.5; }
.ov-box { fill: none; stroke: var(--sel); stroke-width: 1; stroke-dasharray: 4 3; }
.ov-handle { fill: #fff; stroke: var(--sel); stroke-width: 1.5; }
.ov-rot { fill: #fff; stroke: var(--sel); stroke-width: 1.5; }
.ov-node { fill: #fff; stroke: var(--sel); stroke-width: 1.5; }
.ov-node.on { fill: var(--sel); }
.ov-hline { stroke: var(--sel); stroke-width: 1; }
.ov-hdot { fill: #fff; stroke: var(--sel); stroke-width: 1.5; }
.ov-marq { fill: rgba(13, 148, 136, .08); stroke: var(--sel); stroke-width: 1; }
.ov-pen { fill: none; stroke: var(--accent); stroke-width: 1.5; }

#toast { position: absolute; left: 50%; top: 12px; transform: translateX(-50%); background: #1d2430; color: #fff; padding: 8px 14px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 80%; z-index: 5; }
#toast.show { opacity: .96; }
#toast.err { background: var(--danger); }
#status { position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 18px; padding: 5px 12px; background: rgba(255, 255, 255, .92); border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
#stHint { margin-left: auto; }

#panel { grid-area: panel; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; }
#panel section { padding: 12px 14px; border-bottom: 1px solid var(--line); }
#panel h3 { margin: 0 0 10px; font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); display: flex; justify-content: space-between; align-items: center; }
#panel h3 button { padding: 0 8px; line-height: 20px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 label { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.grid2 input { width: 100%; }
.row { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 9px; }
.row button { padding: 4px 8px; font-size: 12px; }
.muted { color: var(--muted); }
.check { display: flex; align-items: center; gap: 6px; margin-top: 8px; color: var(--muted); }

.layer { display: grid; grid-template-columns: 22px 22px 24px 1fr; gap: 4px; align-items: center; padding: 6px; border: 1px solid transparent; border-radius: 8px; }
.layer.active { background: var(--accent-weak); border-color: #cfd3ff; }
.layer .ico { width: 22px; height: 22px; padding: 0; border: none; background: none; opacity: .75; font-size: 14px; line-height: 1; }
.layer .ico.off { opacity: .3; }
.layer input[type=color] { width: 22px; height: 22px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; cursor: pointer; }
.layer .lname { border: 1px solid transparent; background: transparent; font: inherit; padding: 3px 4px; border-radius: 5px; min-width: 0; width: 100%; }
.layer .lname:focus { background: #fff; border-color: var(--line); outline: none; }
.layer .lrow2 { grid-column: 1 / -1; display: flex; gap: 4px; align-items: center; padding-left: 2px; }
.layer select { padding: 2px 4px; font-size: 12px; }
.layer .lrow2 button { padding: 1px 6px; font-size: 12px; }
.layer .lcount { margin-left: auto; color: var(--muted); font-size: 11px; }

/* Everything inside the canvas is drawn only; the root <svg> is the sole event target so
   re-rendering overlays mid-gesture can never swallow click/dblclick events. */
#canvas * { pointer-events: none; }

.tsec, .opsec { margin-bottom: 12px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.opsec { margin: 12px 0 0; padding: 12px 0 0; border-bottom: none; border-top: 1px solid var(--line); }
#panel h4 { margin: 0 0 8px; font-size: 12px; font-weight: 600; }
textarea { width: 100%; font: inherit; border: 1px solid var(--line); border-radius: 6px; padding: 6px; resize: vertical; margin-bottom: 8px; }
select.full { width: 100%; margin-bottom: 8px; }
.oprow { display: flex; gap: 6px; align-items: center; margin-top: 10px; }
.oprow label { display: flex; align-items: center; gap: 5px; color: var(--muted); }
.oprow input { width: 60px; }
.small { font-size: 11px; margin: 8px 0 0; }
#canvas.t-text { cursor: text; }

/* effects + variables */
.fxsec, .bindsec { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.bindsec { margin-top: 0; margin-bottom: 12px; padding-bottom: 12px; border-bottom: 1px solid var(--line); border-top: none; padding-top: 0; }
.fxcard { border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin-bottom: 8px; background: #fafbfc; }
.fxhead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.fxhead button { padding: 0 6px; font-size: 12px; line-height: 20px; }
.fxparams { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 8px; }
.fxparams label { display: flex; flex-direction: column; gap: 2px; color: var(--muted); font-size: 11px; }
.fxparams label.check { flex-direction: row; align-items: center; margin: 0; grid-column: 1 / -1; }
.fxparams input[type=text], .fxparams select { width: 100%; font-size: 12px; }
.fxwarn { color: var(--danger); font-size: 11px; margin-top: 4px; }
.fxwarn:empty { display: none; }
input.bad { border-color: var(--danger); background: #fff5f5; }
code { background: #f1f3f7; padding: 0 4px; border-radius: 4px; font-size: 11px; }
.var { display: grid; grid-template-columns: 1fr 70px 24px; gap: 4px 6px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.var:last-child { border-bottom: none; }
.var .vname { font-family: ui-monospace, Menlo, Consolas, monospace; width: 100%; border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; }
.var .vval { width: 100%; }
.var input[type=range] { grid-column: 1 / -1; width: 100%; accent-color: var(--accent); }
.var .vrange { grid-column: 1 / -1; display: flex; gap: 6px; color: var(--muted); font-size: 11px; }
.var .vrange label { display: flex; align-items: center; gap: 3px; }
.var .vrange input { width: 100%; min-width: 0; padding: 2px 4px; font-size: 11px; }
.var button { padding: 0; width: 24px; height: 24px; }

/* ---- Phase 4: dialogs, account, cloud ---- */
.cloudstate { font-size: 12px; color: var(--muted); min-width: 0; }
.cloudstate.dirty { color: #b45309; }
dialog { border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; width: min(440px, 92vw); color: var(--text); box-shadow: 0 20px 60px rgba(20, 24, 40, .25); }
dialog::backdrop { background: rgba(20, 24, 40, .35); }
dialog h3 { margin: 0 0 10px; font-size: 16px; }
dialog h4 { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
dialog label { display: flex; flex-direction: column; gap: 3px; margin: 8px 0; font-size: 12px; color: var(--muted); }
dialog label input[type=checkbox] { align-self: flex-start; }
dialog input, dialog select { font: inherit; color: var(--text); padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; }
dialog label:has(> input[type=checkbox]) { flex-direction: row; align-items: center; gap: 6px; }
.dlgbtns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; align-items: center; }
.dlgbtns .spacer { flex: 1; }
.dlgerr { color: var(--danger); font-size: 12px; min-height: 16px; margin: 6px 0 0; }
.tabs { display: flex; gap: 4px; margin-bottom: 6px; }
.tabs button { flex: 1; border-radius: 7px; }
.tabs button.on { background: var(--accent-weak); border-color: var(--accent); color: var(--accent); }
.prlist { display: flex; flex-direction: column; gap: 4px; max-height: 300px; overflow: auto; }
.prrow { display: grid; grid-template-columns: 1fr auto auto; gap: 6px; align-items: center; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.prrow.cur { border-color: var(--accent); background: var(--accent-weak); }
.prrow .pn { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prrow .pm { grid-column: 1; font-size: 11px; color: var(--muted); }
.prrow button { padding: 3px 8px; }
button.danger { color: var(--danger); }

/* ---- Phase 5: gallery ---- */
dialog.wide { width: min(980px, 95vw); max-height: 90vh; overflow: auto; }
.glhead { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.glhead h3 { margin: 0; }
.glhead .spacer { flex: 1; }
label.inline { flex-direction: row; align-items: center; gap: 4px; margin: 0; }
.glgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.glcard { text-align: left; padding: 0; overflow: hidden; border-radius: 10px; display: flex; flex-direction: column; }
.glcard:hover { border-color: var(--accent); }
.glcard img { width: 100%; aspect-ratio: 4 / 3; object-fit: contain; background: #fff; border-bottom: 1px solid var(--line); }
.glcard .gc { padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; }
.glcard .gt { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.glcard .gm { font-size: 11px; color: var(--muted); }
.badge { display: inline-block; font-size: 10px; text-transform: uppercase; letter-spacing: .04em; padding: 1px 6px; border-radius: 999px; background: #fef3c7; color: #92400e; width: fit-content; }
.badge.removed { background: #fee2e2; color: #991b1b; }
.glpager { display: flex; gap: 10px; align-items: center; justify-content: center; margin-top: 12px; }
.gdbody { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 18px; margin-top: 10px; }
.gdbody img { width: 100%; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
.dlgbtns.left { justify-content: flex-start; flex-wrap: wrap; }
#gdDesc { white-space: pre-wrap; }
dialog textarea { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; resize: vertical; }
.pbimg { width: 100%; max-height: 220px; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
.adcard { border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: grid; grid-template-columns: 140px 1fr; gap: 12px; }
.adcard img { width: 140px; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
.adcard .rep { font-size: 12px; margin: 2px 0; }
.adcard .acts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
@media (max-width: 700px) { .gdbody, .adcard { grid-template-columns: 1fr; } }

/* ---- polish ---- */
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#emptyHint { position: absolute; left: 50%; bottom: 44px; transform: translateX(-50%); text-align: center; display: flex; flex-direction: column; gap: 8px; align-items: center; color: var(--muted); background: rgba(255,255,255,.92); border: 1px dashed var(--line); border-radius: 12px; padding: 16px 22px; z-index: 2; pointer-events: none; }
#emptyHint button { pointer-events: auto; }
#emptyHint[hidden] { display: none; }
#emptyHint b { color: var(--text); font-size: 15px; }
#emptyHint .acts { display: flex; gap: 8px; }
kbd { font: 600 11px ui-monospace, monospace; background: #f3f4f8; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 1px 5px; }
table.keys { border-collapse: collapse; font-size: 12px; }
table.keys th { text-align: left; vertical-align: top; padding: 5px 12px 5px 0; color: var(--muted); white-space: nowrap; }
table.keys td { padding: 5px 0; }
.alignsec h4 { margin: 10px 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.alignsec .row button { min-width: 30px; }

/* ---- Phase 6: billing ---- */
.plgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; margin: 8px 0 12px; }
.plcard { border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.plcard.cur { border-color: var(--accent); background: var(--accent-weak); }
.plcard h4 { margin: 0; font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--text); }
.plcard ul { margin: 6px 0; padding-left: 18px; }
.plcard .pbtns { display: flex; flex-direction: column; gap: 6px; margin-top: auto; padding-top: 8px; }

.stbanner { padding: 8px 10px; border: 1px solid var(--border, #ccd); border-radius: 8px; margin: 8px 0; font-size: 13px; }
.stbanner.warn { border-color: #c80; background: #fff7e6; }
.adplan { border: 1px solid var(--border, #ccd); border-radius: 10px; padding: 10px 12px; margin: 10px 0; }
.adplanhead { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.adplanhead input { font-weight: 600; max-width: 180px; }
.plfields { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
.plfields label { display: flex; flex-direction: column; gap: 2px; }
.adprices { margin: 10px 0 6px; display: flex; flex-direction: column; gap: 6px; }
.adpr { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pluginbar { display: contents; }
.pluginbar button { border-style: dashed; }
#dlgAdmin h4 { margin: 14px 0 4px; }
