* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    overflow: hidden;
    background: #f5f5f5;
    user-select: none;
    /* Prevent text selection during manipulation */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Allow text selection in inputs and textareas */
input,
textarea {
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
}

/* Toolbar */
.toolbar {
    position: fixed;
    left: 20px;
    top: 20px;
    background: white;
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tool-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    border-radius: 4px;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.tool-btn:hover {
    background: #f0f0f0;
}

.tool-btn.active {
    background: #e3f2fd;
    color: #1976d2;
}

.separator {
    height: 1px;
    background: #e0e0e0;
    margin: 4px 0;
}

/* Print Settings Panel */
.print-settings {
    position: fixed;
    right: 20px;
    top: 20px;
    background: white;
    border-radius: 8px;
    padding: 16px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    width: 280px;
    display: none;
}

.print-settings.visible {
    display: block;
}

.settings-header {
    font-weight: bold;
    font-size: 14px;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid #e0e0e0;
}

.print-settings label {
    display: block;
    font-size: 12px;
    color: #666;
    margin-top: 12px;
    margin-bottom: 4px;
}

.print-settings select {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 12px;
    background: white;
    cursor: pointer;
}

.orientation-buttons {
    display: flex;
    gap: 8px;
}

.orientation-btn {
    flex: 1;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: white;
    font-size: 11px;
    cursor: pointer;
    transition: all 0.2s;
}

.orientation-btn:hover {
    background: #f0f0f0;
}

.orientation-btn.active {
    background: #e3f2fd;
    border-color: #1976d2;
    color: #1976d2;
}

.size-inputs {
    display: flex;
    align-items: center;
    gap: 8px;
}

.size-inputs input {
    flex: 1;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 12px;
}

.size-inputs span {
    color: #999;
    font-size: 14px;
}

/* Pages Management Section */
.pages-section {
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid #e0e0e0;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.section-header span {
    font-weight: 600;
    font-size: 13px;
    color: #333;
}

.add-page-btn {
    padding: 4px 10px;
    font-size: 11px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
}

.add-page-btn:hover {
    background: #2563eb;
}

.all-pages-control {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 0;
    margin-bottom: 4px;
    border-bottom: 1px solid #f0f0f0;
}

.all-pages-control input[type="checkbox"] {
    cursor: pointer;
}

.all-pages-control label {
    margin: 0;
    font-size: 12px;
    font-weight: 600;
    color: #555;
    cursor: pointer;
}

.pages-list {
    max-height: 200px;
    overflow-y: auto;
}

.page-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 4px;
    border-radius: 3px;
    transition: background 0.2s;
}

.page-item:hover {
    background: #f8f8f8;
}

.page-item-checkbox {
    cursor: pointer;
}

.page-name {
    flex: 1;
    font-size: 12px;
    color: #333;
}

.page-item-btn {
    padding: 2px 6px;
    font-size: 14px;
    background: transparent;
    border: none;
    cursor: pointer;
    border-radius: 3px;
    transition: background 0.2s;
}

.page-item-btn:hover {
    background: #e0e0e0;
}

.delete-page-btn {
    color: #ef4444;
}

.delete-page-btn:hover {
    background: #fee2e2;
}

/* Canvas */
#canvas {
    width: 100vw;
    height: 100vh;
    position: relative;
    overflow: hidden;
    cursor: grab;
}

#canvas.grabbing {
    cursor: grabbing;
}

#grid-background {
    position: absolute;
    width: 100%;
    height: 100%;
    background-image:
        linear-gradient(rgba(0, 0, 0, 0.05) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0, 0, 0, 0.05) 1px, transparent 1px);
    background-size: 20px 20px;
    pointer-events: none;
}

#world {
    position: absolute;
    transform-origin: 0 0;
    width: 4000px;
    height: 4000px;
}


.connection-line {
    fill: none;
    stroke: #9ca3af;
    stroke-width: 2;
    pointer-events: stroke;
    cursor: pointer;
}

.connection-line:hover,
.connection-line.selected-line {
    stroke: #3b82f6;
    stroke-width: 3;
}

/* Nodes */
.board-item {
    position: absolute;
    background: #fef3c7;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    cursor: move;
    min-width: 200px;
    padding: 12px;
    z-index: 20;
}

.board-item.selected {
    box-shadow: 0 0 0 2px #3b82f6;
}

.board-item.dragging {
    opacity: 0.8;
}

.board-item textarea {
    width: 100%;
    border: none;
    background: transparent;
    resize: none;
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

/* Note Node */
.board-item.note {
    min-height: 40px;
}

/* Simple Text Node */
.board-item.text {
    min-height: 30px;
}

/* Result Node */
.board-item.result {
    min-width: 250px;
}

.result-title-input {
    width: calc(100% - 10px);
    border: 1px solid #e2e8f0;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.5);
    font-weight: bold;
    font-size: 14px;
    margin-bottom: 8px;
    padding: 6px 8px;
    outline: none;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.02);
}

.result-rows {
    display: flex;
    flex-direction: column;
    gap: 2px;
    font-size: 12px;
}

.result-row-item {
    display: grid;
    gap: 8px;
    padding: 2px 0;
}

.result-row-item.result-header {
    font-weight: bold;
    border-bottom: 1px solid #aaa;
    margin-bottom: 4px;
    padding-bottom: 4px;
}

.result-row-item.result-grand-total {
    border-top: 2px solid #000;
    margin-top: 4px;
    padding-top: 4px;
    font-weight: bold;
}

/* Calc Rows */
.calc-rows-container {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 8px 0;
}

.calc-row {
    display: grid;
    grid-template-columns: 24px 1fr 104px 24px;
    gap: 6px;
    align-items: center;
}

.calc-row-index {
    font-size: 11px;
    color: #666;
}

.calc-label {
    border: 1px solid #e2e8f0;
    border-radius: 3px;
    padding: 5px 8px;
    font-size: 12px;
    background: white;
    outline: none;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.02);
}

.value-input {
    border: 1px solid #e2e8f0;
    border-radius: 3px;
    padding: 5px 8px;
    font-size: 12px;
    text-align: right;
    background: white;
    outline: none;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.02);
    -moz-appearance: textfield;
    /* Firefox */
    appearance: textfield;
}

/* Chrome, Safari, Edge, Opera */
.value-input::-webkit-outer-spin-button,
.value-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.row-delete-btn {
    cursor: pointer;
    color: #ef4444;
    font-weight: bold;
    text-align: center;
    user-select: none;
}

.row-delete-btn:hover {
    color: #dc2626;
}

/* Text Rows */
.text-rows-container {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin: 4px 0;
}

.text-row {
    display: grid;
    grid-template-columns: 24px 1fr 104px 24px;
    gap: 6px;
    align-items: center;
}

.text-row-input {
    border: 1px solid #e2e8f0;
    border-radius: 3px;
    padding: 5px 8px;
    font-size: 12px;
    background: white;
    outline: none;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.02);
}

.text-row-buttons {
    display: flex;
    gap: 4px;
    width: 100%;
}

.text-row-btn {
    border: 1px solid #e2e8f0;
    border-radius: 3px;
    padding: 2px 0;
    font-size: 11px;
    font-weight: bold;
    background: white;
    cursor: pointer;
    flex: 1;
    /* 50/50 split */
    text-align: center;
}

.text-row-btn:hover {
    background: #f0f0f0;
}

/* Toolbar in nodes */
.node-toolbar {
    display: none;
    gap: 4px;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.board-item:hover .node-toolbar {
    display: flex;
}

.node-toolbar button {
    flex: 1;
    padding: 4px 8px;
    font-size: 11px;
    border: 1px solid #ddd;
    border-radius: 3px;
    background: white;
    cursor: pointer;
}

.node-toolbar button:hover {
    background: #f0f0f0;
}

/* Delete button */
.delete-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #ef4444;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 12px;
    display: none;
    align-items: center;
    justify-content: center;
}

.board-item:hover .delete-btn {
    display: flex;
}

.delete-btn:hover {
    background: #dc2626;
}

/* Color Picker Button */
.color-picker-btn {
    position: absolute;
    top: 4px;
    right: 30px;
    width: 24px;
    height: 24px;
    border: 2px solid white;
    border-radius: 4px;
    cursor: pointer;
    display: none;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.board-item:hover .color-picker-btn {
    display: block;
}

/* Color Palette Popup */
.color-palette {
    position: absolute;
    top: 32px;
    right: 30px;
    background: white;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    padding: 12px;
    z-index: 1000;
    min-width: 220px;
}

.palette-title {
    font-size: 11px;
    font-weight: 600;
    color: #666;
    margin-bottom: 8px;
    letter-spacing: 0.5px;
}

.palette-subtitle {
    font-size: 11px;
    font-weight: 600;
    color: #666;
    margin: 12px 0 8px 0;
    letter-spacing: 0.5px;
}

.color-grid {
    display: grid;
    grid-template-columns: repeat(8, 24px);
    gap: 4px;
}

.color-grid.standard {
    grid-template-columns: repeat(10, 20px);
    gap: 3px;
}

.color-swatch {
    width: 24px;
    height: 24px;
    border-radius: 2px;
    cursor: pointer;
    border: 1px solid rgba(0, 0, 0, 0.1);
    transition: transform 0.1s, box-shadow 0.1s;
}

.color-grid.standard .color-swatch {
    width: 20px;
    height: 20px;
}

.color-swatch:hover {
    transform: scale(1.15);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    z-index: 1;
}

/* Print Frame Overlay */
.print-frame {
    position: absolute;
    border: 2px dashed #3b82f6;
    background: rgba(59, 130, 246, 0.03);
    pointer-events: none;
    /* Default: click-through */
    z-index: 50;
    display: none;
}

.print-frame.visible {
    display: block;
}

/* Allow pointer events on the frame border/children */
.print-frame.visible {
    pointer-events: auto;
    /* Make the whole frame interactable */
    cursor: move;
    /* Show move cursor over the whole area */
}

/* Ghost Mode specialized styling */
.print-frame.ghost {
    border: 1.5px dashed rgba(59, 130, 246, 0.3);
    background: rgba(59, 130, 246, 0.01);
    pointer-events: none !important;
    z-index: 5;
    /* Sit behind nodes but above canvas */
}

.print-frame.ghost .print-margin-line {
    border-color: rgba(59, 130, 246, 0.15);
}

.tool-btn#toggle-ghost-print.active {
    background: #3b82f6;
    color: white;
    border-color: #2563eb;
}

.print-drag-handle {
    position: absolute;
    top: -24px;
    left: 0;
    height: 24px;
    background: #3b82f6;
    color: white;
    padding: 0 8px;
    border-radius: 4px 4px 0 0;
    font-size: 11px;
    display: flex;
    align-items: center;
    cursor: grab;
    pointer-events: auto;
    /* Enable clicks */
    white-space: nowrap;
    z-index: 51;
}

.print-drag-handle:active {
    cursor: grabbing;
    background: #2563eb;
}

.print-label {
    position: absolute;
    bottom: -30px;
    right: 0;
    background: #3b82f6;
    color: white;
    padding: 4px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.print-corner {
    position: absolute;
    width: 20px;
    height: 20px;
    border: 3px solid #3b82f6;
}

.print-corner.tl {
    top: -2px;
    left: -2px;
    border-right: none;
    border-bottom: none;
}

.print-corner.tr {
    top: -2px;
    right: -2px;
    border-left: none;
    border-bottom: none;
}

.print-corner.bl {
    bottom: -2px;
    left: -2px;
    border-right: none;
    border-top: none;
}

.print-corner.br {
    bottom: -2px;
    right: -2px;
    border-left: none;
    border-top: none;
}

/* Margin Settings UI */
.margin-settings-label {
    font-size: 11px;
    color: #666;
    font-weight: 500;
}

.margin-label-row {
    grid-column: span 3;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 8px;
}

.borderless-btn {
    background: #e2e8f0;
    border: none;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 9px;
    cursor: pointer;
    color: #475569;
    font-weight: 600;
    transition: all 0.2s;
}

.borderless-btn:hover {
    background: #3b82f6;
    color: white;
}

.margin-grid {
    grid-column: span 3;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    margin-top: 4px;
}

.margin-input-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.margin-input-wrapper span {
    font-size: 9px;
    color: #999;
}

.margin-input {
    width: 35px;
    border: 1px solid #ddd;
    border-radius: 2px;
    padding: 2px;
    font-size: 10px;
    text-align: center;
}

.margin-input:focus {
    border-color: #3b82f6;
    outline: none;
}

.page-item {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 4px;

}

#connections-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
    z-index: 10;
}