* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg-color);
    color: var(--text-color);
    font: 14px 'Outfit', system-ui, sans-serif;
}

button,
input,
select,
textarea {
    font: inherit;
}

button {
    padding: 8px 11px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--surface-control);
    color: var(--text-color);
    cursor: pointer;
}

button:hover {
    border-color: var(--border-hover);
    background: var(--surface-hover);
}

button.primary {
    border-color: var(--cyan);
    background: var(--cyan);
    color: var(--on-accent);
    font-weight: 700;
}

button.primary:hover {
    filter: brightness(1.08);
}

:is(a,
button,
input,
select,
textarea):focus-visible {
    outline: 2px solid var(--cyan);
    outline-offset: 2px;
}

[hidden] {
    display: none !important;
}

.editor-header {
    min-height: 64px;
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 12px 24px;
    background: var(--nav-bg);
    border-bottom: 1px solid var(--border-color);
}

.editor-header h1 {
    margin: 0;
    font-size: 19px;
    line-height: 1.15;
}

#active-template { display: block; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 400; color: var(--text-subtle); margin-top: 3px; }

.editor-header a {
    color: var(--cyan);
    text-decoration: none;
    white-space: nowrap;
}

#status {
    margin-left: auto;
    color: var(--text-subtle);
}

.editor-layout {
    display: grid;
    grid-template-columns: 300px minmax(0, 1fr) 340px;
    min-height: calc(100vh - 64px);
}

@media (min-width: 951px) {
    .editor-layout { height: calc(100dvh - 64px); }
}

.panel {
    padding: 20px;
    background: var(--panel-bg);
    overflow: auto;
}

.controls {
    border-right: 1px solid var(--border-color);
}

.command-nav { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 3px; position: sticky; top: -20px; z-index: 5; margin: -20px -20px 20px; padding: 12px 10px; background: var(--bg-color); border-bottom: 1px solid var(--border-color); }
.command-nav button { padding: 8px 2px; border-color: transparent; background: transparent; font-size: 12px; }
.command-nav button[aria-selected="true"] { background: var(--surface-alt); border-color: var(--border-color); color: var(--cyan); font-weight: 700; }
.template-name { margin-top: 20px; }
.controls h3 { margin: 20px 0 12px; font-size: 13px; }
.selection-empty { padding: 24px 16px; border: 1px dashed var(--border-color); border-radius: 10px; color: var(--text-subtle); line-height: 1.6; text-align: center; }
button.danger { color: var(--orange); }

.properties {
    border-left: 1px solid var(--border-color);
}

h2 {
    margin: 0 0 16px;
    font-size: 15px;
}

label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 14px;
    color: var(--text-soft);
}

input:not([type=checkbox]):not([type=range]),
select,
textarea {
    min-width: 0;
    width: 100%;
    padding: 8px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--surface-input);
    color: var(--text-color);
}

textarea {
    min-height: 64px;
    resize: vertical;
}

.outlined-field { position: relative; gap: 0; margin-top: 8px; }
.outlined-field > .field-caption { position: absolute; z-index: 1; top: -7px; left: 9px; max-width: calc(100% - 24px); padding: 0 5px; background: var(--panel-bg); color: var(--text-subtle); font-size: 12px; line-height: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.outlined-field > :is(input, select, textarea) { min-height: 42px; padding-top: 11px; padding-bottom: 9px; }
.outlined-field:focus-within > .field-caption { color: var(--cyan); }
.toolbar-option-content .outlined-field > .field-caption { background: var(--panel-bg); }

.field-insertion-row { display: flex; align-items: center; gap: 8px; margin: 8px 0 14px; }
.field-insertion-controls { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.field-insertion-controls > label { flex: 1; min-width: 0; margin: 0; }
.field-combination { margin: 8px 0 20px; }
.field-combination .checkbox-label { display: flex; flex-direction: row; align-items: center; gap: 8px; }
.field-combination input[type="checkbox"] { width: auto; }
#field-format-control { margin-top: 16px; }
.element-picker { align-items: flex-end; }
.element-choices { flex: 1; min-width: 0; display: grid; gap: 16px; }
.element-choices > label { margin: 0; }
.field-icon-button { width: 36px; height: 42px; flex: 0 0 36px; padding: 0; font-size: 22px; line-height: 1; }
.field-icon-button.remove-icon-button { position: relative; font-size: 0; }
.field-icon-button.remove-icon-button::before { content: ""; position: absolute; left: 50%; top: 50%; width: 12px; height: 2px; border-radius: 1px; background: currentColor; transform: translate(-50%, -50%); }
.collection-field { display: flex; align-items: center; gap: 6px; min-width: 0; grid-column: 1 / -1; }
.collection-field > label { flex: 1; min-width: 0; }
#document-properties summary > .field-icon-button { float: right; height: 26px; width: 26px; margin: -3px 0; }
.template-picker-row { display: flex; align-items: center; gap: 8px; margin: 8px 0 14px; }
.template-picker-row > label { flex: 1; min-width: 0; margin: 0; }

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
}

.hint {
    color: var(--text-subtle);
    font-size: 12px;
    line-height: 1.5;
}

ol {
    padding: 0;
    list-style: none;
}

li button {
    width: 100%;
    margin: 3px 0;
    text-align: left;
}

li button.active {
    outline: 2px solid var(--cyan);
}

.stage {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg-color);
}

.toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 18px;
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border-color);
}

.toolbar label {
    margin: 0;
}

.toolbar .inline-label {
    flex-direction: row;
    align-items: center;
    align-self: center;
}

.toolbar select {
    width: auto;
}

.toolbar input[type=range] {
    width: 80px;
}

.toolbar-group { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.toolbar .zoom-control { flex-direction: row; align-items: center; gap: 8px; }
#zoom-value { min-width: 4ch; text-align: right; }
.toolbar > button, .toolbar-group > button, .toolbar-options summary { min-height: 38px; display: inline-flex; align-items: center; font-size: 13px; line-height: 20px; }
.toolbar-options { position: relative; margin: 0; padding: 0; border: 0; align-self: center; }
.toolbar-options summary { margin: 0; padding: 8px 10px; border: 1px solid var(--border-color); border-radius: 8px; font-size: 13px; }
.toolbar-option-content { position: absolute; top: calc(100% + 8px); left: 0; z-index: 20; min-width: 230px; display: grid; gap: 16px; padding: 16px; background: var(--panel-bg); border: 1px solid var(--border-color); border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.25); }
.toolbar-option-content label { align-self: auto; }
.toolbar-option-content input:not([type=checkbox]) { width: 100%; }

#preview-ean {
    width: 150px;
}

.scroller {
    flex: 1;
    min-height: 0;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    overflow: auto;
    padding: 40px;
}

#canvas {
    position: relative;
    flex: none;
    margin-inline: auto;
    background: white;
    box-shadow: var(--card-glow);
    touch-action: none;
}

#rendered,
#overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}

#rendered {
    image-rendering: pixelated;
}

#rendered:not([src]) {
    display: none;
}

#overlay {
    pointer-events: none;
}

.grid #overlay {
    background-image: linear-gradient(to right, #2684bb38 1px, transparent 1px), linear-gradient(to bottom, #2684bb38 1px, transparent 1px);
    background-size: 4px 4px;
}

.node-box {
    position: absolute;
    cursor: move;
    pointer-events: auto;
    touch-action: none;
}

.node-box.selected {
    border-color: #c47a00;
    background: #f0ad4822;
}

.handle {
    display: none;
    position: absolute;
    z-index: 1;
    width: 12px;
    height: 12px;
    border: 1px solid #fff;
    background: #c47a00;
}

.node-box.selected .handle {
    display: block;
}

.handle[data-direction="n"] {
    top: -7px;
    left: 50%;
    transform: translateX(-50%);
    cursor: ns-resize;
}

.handle[data-direction="s"] {
    bottom: -7px;
    left: 50%;
    transform: translateX(-50%);
    cursor: ns-resize;
}

.handle[data-direction="e"] {
    right: -7px;
    top: 50%;
    transform: translateY(-50%);
    cursor: ew-resize;
}

.handle[data-direction="w"] {
    left: -7px;
    top: 50%;
    transform: translateY(-50%);
    cursor: ew-resize;
}

.handle[data-direction="ne"] {
    top: -7px;
    right: -7px;
    cursor: nesw-resize;
}

.handle[data-direction="sw"] {
    bottom: -7px;
    left: -7px;
    cursor: nesw-resize;
}

.handle[data-direction="nw"] {
    top: -7px;
    left: -7px;
    cursor: nwse-resize;
}

.handle[data-direction="se"] {
    bottom: -7px;
    right: -7px;
    cursor: nwse-resize;
}

.stage-hint {
    padding: 0 20px;
}

#notice {
    color: var(--orange);
}

@media (max-width: 950px) {
    .editor-layout {
        grid-template-columns: 210px minmax(0, 1fr);
    }

    .properties {
        grid-column: 1 / -1;
        max-height: 260px;
        border-left: 0;
        border-top: 1px solid var(--border-color);
    }

    .stage {
        min-height: 500px;
    }
}

@media (max-width: 700px) {
    .command-nav { top: -16px; margin: -16px -16px 16px; }
    .toolbar-options { position: static; }
    .toolbar { position: relative; }
    .toolbar-option-content { left: 16px; right: 16px; width: auto; }
    .editor-header {
        flex-wrap: wrap;
        gap: 8px 16px;
        padding: 12px 16px;
    }

    #status {
        flex-basis: 100%;
        margin-left: 0;
    }

    .editor-layout {
        display: flex;
        flex-direction: column;
    }

    .panel {
        min-width: 0;
        width: 100%;
        overflow: visible;
        padding: 16px;
    }

    .hint {
        overflow-wrap: anywhere;
    }

    .controls,
    .properties {
        max-height: none;
        border: 0;
        border-bottom: 1px solid var(--border-color);
    }

    .stage {
        min-height: 420px;
    }

    .scroller {
        min-height: 320px;
        justify-content: flex-start;
        padding: 20px;
    }

    .toolbar {
        padding: 12px 16px;
    }
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

button:disabled:hover {
    border-color: var(--border-color);
    background: var(--surface-control);
}

#document-state {
    min-height: 18px;
    margin: -4px 0 14px;
}

.layer-actions {
    margin: 14px 0;
}

.header-actions {
    display: flex;
    gap: 7px;
}

.header-actions button {
    padding: 6px 9px;
}

#layers {
    display: grid;
    gap: 4px;
}

#layers li button {
    margin: 0;
}

#layers li button.active {
    background: var(--surface-hover);
}

#text-options[hidden] {
    display: none;
}

.panel {
    max-height: calc(100vh - 64px);
}

h2:not(:first-child) {
    margin-top: 24px;
}

details {
    margin: 16px 0;
    border-top: 1px solid var(--border-color);
    padding-top: 10px;
}

summary {
    cursor: pointer;
    color: var(--text-soft);
    margin-bottom: 12px;
}

.property-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0 12px;
}

.property-grid .wide {
    grid-column: 1 / -1;
}

.property-grid label {
    font-size: 12px;
}

.property-grid small {
    overflow-wrap: anywhere;
    color: var(--text-subtle);
}

.json-value,
#template-json,
#node-json {
    min-height: 130px;
    font: 12px ui-monospace, monospace;
}

#template-json {
    min-height: 260px;
}

#layers ol {
    margin: 0 0 0 14px;
    border-left: 1px solid var(--border-color);
    padding-left: 6px;
}

.layer-row {
    display: flex;
    gap: 4px;
    align-items: center;
}

.layer-row .layer-select {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
}

.layer-row .layer-toggle,
.layer-row .layer-lock {
    width: 28px;
    flex: none;
    padding: 6px 2px;
    text-align: center;
}

.node-box {
    border: 1px solid transparent;
    background: transparent;
}

.node-box:hover,
#canvas.show-bounds .node-box {
    border-color: #1685d8;
}

.node-box.selected {
    border: 2px solid #c47a00;
}

.node-box.readonly {
    cursor: pointer;
}

.node-box.group-box {
    border-style: dashed;
}

.node-box.repeated-block { border: 2px dashed #a56bff; }
.node-box.repeated-block.selected { border-color: #a56bff; background: #a56bff15; }

#warnings {
    padding: 0 20px 14px;
    color: var(--orange);
    font-size: 12px;
    white-space: pre-line;
}

#preview-barcode {
    width: 140px;
}

@media (max-width: 1100px) {
    .editor-layout {
        grid-template-columns: 240px minmax(0,1fr);
    }

    .properties {
        grid-column: 1 / -1;
        max-height: 440px;
    }
}

@media (max-width: 700px) {
    .panel {
        max-height: none;
    }
}

.color-selector {
    min-width: 0;
    margin: 0 0 14px;
    padding: 0;
    border: 0;
}

.color-selector legend {
    margin-bottom: 7px;
    color: var(--text-soft);
    font-size: 12px;
}

.color-swatches {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
}

.color-swatch {
    display: flex;
    align-items: center;
    flex-direction: column;
    gap: 5px;
    padding: 7px 3px;
    font-size: 11px;
}

.color-swatch[aria-pressed="true"] {
    outline: 2px solid var(--cyan);
    outline-offset: 1px;
}

.swatch-chip {
    width: 24px;
    height: 18px;
    border: 1px solid var(--text-subtle);
    border-radius: 3px;
}

[data-color="black"] .swatch-chip {
    background: #000;
}

[data-color="white"] .swatch-chip {
    background: #fff;
}

[data-color="red"] .swatch-chip {
    background: #f00;
}

[data-color="yellow"] .swatch-chip {
    background: #ff0;
}

#background-none { grid-column: 1 / -1; flex-direction: row; justify-content: center; }
[data-color="none"] .swatch-chip { background: repeating-conic-gradient(#fff 0% 25%, #aaa 0% 50%) 0 0 / 8px 8px; }

.field-library {max-height:280px;overflow:auto;border:1px solid var(--border-color);border-radius:10px;padding:8px;background:var(--surface-input);}
.field-category-title {margin:10px 4px 6px;color:var(--cyan);font-size:11px;font-weight:700;letter-spacing:.04em;}
.field-divider { margin: 10px 0; border: 0; border-top: 1px solid var(--border-color); }
.field-choice {flex-direction:row;align-items:center;gap:10px;padding:8px;margin:0;border-radius:6px;cursor:pointer;}
.field-choice:hover {background:var(--surface-hover);}
.field-choice:has(input:checked) {background:var(--surface-alt);outline:1px solid var(--cyan);}
.field-choice input {flex:none;accent-color:var(--cyan);}
.field-choice span {display:grid;gap:3px;min-width:0;}
.field-choice strong {font-size:12px;font-weight:600;}
.field-choice small {overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11px;color:var(--text-subtle);}
