body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: #f8f9fa;
    user-select: none;
    overflow: hidden; /* Prevent body scroll */
    --digits-font-size: 12px; /* default: fits ~999.9 inside nodes */
}

header {
    background-color: rgba(255, 255, 255, 0.95);
    border-bottom: 1px solid #ddd;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    z-index: 100; /* Keep above canvas */
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
}

.toolbar {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

#canvas-hint {
    position: absolute;
    top: 10px;
    right: 12px;
    padding: 6px 10px;
    border-radius: 8px;
    background: rgba(255,255,255,0.75);
    border: 1px solid rgba(0,0,0,0.08);
    color: rgba(0,0,0,0.55);
    font-size: 0.85rem;
    user-select: none;
    pointer-events: none;
    display: none; /* shown only in Select mode via JS */
}

h1 {
    margin: 0;
    font-size: 1.1rem;
    color: #222;
}

button {
    padding: 6px 10px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background-color 0.15s;
}

button:hover { background-color: #0056b3; }
button:disabled { background-color: #c6c6c6; cursor: not-allowed; }

button.secondary { background-color: #6c757d; }
button.secondary:hover { background-color: #5a6268; }

button.danger { background-color: #dc3545; }
button.danger:hover { background-color: #bd2130; }

.separator { color: #bbb; user-select: none; }

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: #333;
    padding: 4px 8px;
    border-radius: 6px;
    background: rgba(0,0,0,0.03);
    user-select: none;
}

.toggle input { transform: translateY(1px); }

/* Adjust main container to take full height, header overlays it */
.main-container {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
    height: 100vh;
    padding-top: 60px; /* Space for header */
}

#tree-container {
    flex: 1;
    background-color: #f8f9fa;
    position: relative;
    cursor: grab;
    overflow: hidden;
    /* Dot Grid Pattern */
    background-image: radial-gradient(#ccc 1px, transparent 1px);
    background-size: 20px 20px;
}

/* Tool cursors (applied via JS by toggling classes on #tree-container) */
#tree-container.cursor-hand { cursor: grab; }
#tree-container.cursor-hand:active { cursor: grabbing; }

/* SVG cursors via data-URI (small + lightweight) */
#tree-container.cursor-pen-black {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M6%2026l2-8%2012-12%206%206-12%2012-8%202z'%20fill='%23222'/%3E%3Cpath%20d='M21%206l5%205'%20stroke='%23fff'%20stroke-width='2'%20stroke-linecap='round'/%3E%3C/svg%3E") 4 26, auto;
}
#tree-container.cursor-pen-black svg,
#tree-container.cursor-pen-black svg * {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M6%2026l2-8%2012-12%206%206-12%2012-8%202z'%20fill='%23222'/%3E%3Cpath%20d='M21%206l5%205'%20stroke='%23fff'%20stroke-width='2'%20stroke-linecap='round'/%3E%3C/svg%3E") 4 26, auto !important;
}

#tree-container.cursor-pen-red {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M6%2026l2-8%2012-12%206%206-12%2012-8%202z'%20fill='%23e11d2e'/%3E%3Cpath%20d='M21%206l5%205'%20stroke='%23fff'%20stroke-width='2'%20stroke-linecap='round'/%3E%3C/svg%3E") 4 26, auto;
}
#tree-container.cursor-pen-red svg,
#tree-container.cursor-pen-red svg * {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M6%2026l2-8%2012-12%206%206-12%2012-8%202z'%20fill='%23e11d2e'/%3E%3Cpath%20d='M21%206l5%205'%20stroke='%23fff'%20stroke-width='2'%20stroke-linecap='round'/%3E%3C/svg%3E") 4 26, auto !important;
}

#tree-container.cursor-eraser {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M10%2010l10-6%207%207-10%206-7-7z'%20fill='%236c757d'/%3E%3Cpath%20d='M6%2024h18'%20stroke='%236c757d'%20stroke-width='4'%20stroke-linecap='round'/%3E%3C/svg%3E") 6 26, auto;
}
#tree-container.cursor-eraser svg,
#tree-container.cursor-eraser svg * {
    cursor: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='32'%20height='32'%20viewBox='0%200%2032%2032'%3E%3Cpath%20d='M10%2010l10-6%207%207-10%206-7-7z'%20fill='%236c757d'/%3E%3Cpath%20d='M6%2024h18'%20stroke='%236c757d'%20stroke-width='4'%20stroke-linecap='round'/%3E%3C/svg%3E") 6 26, auto !important;
}

#selection-rect {
    position: absolute;
    border: 2px dashed rgba(0, 123, 255, 0.9);
    background: rgba(0, 123, 255, 0.12);
    pointer-events: none;
    display: none;
    z-index: 50;
}

#tree-container:active {
    cursor: grabbing;
}

/* SVG Styles */
svg {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible; /* Allow content to go outside bounds visually if needed, though viewport clips */
}

.node circle, .node rect, .node polygon {
    fill: #fff;
    stroke: #333;
    stroke-width: 2px;
    cursor: pointer;
    transition: fill 0.2s, stroke 0.2s;
}

/* Tree color mode: border colors by node type */
#tree-container.tree-colored .node circle,
#tree-container.tree-colored .node rect,
#tree-container.tree-colored .node polygon {
    stroke: var(--node-stroke, #333);
}

#tree-container.tree-colored .node.selected circle,
#tree-container.tree-colored .node.selected rect,
#tree-container.tree-colored .node.selected polygon {
    fill: #e6f2ff;
    stroke: var(--node-stroke, #333);
    stroke-width: 3px;
}

#tree-container.tree-colored .node.type-decision,
#tree-container.tree-colored .node.root-node {
    --node-stroke: #f1c40f; /* yellow */
}

#tree-container.tree-colored .node.type-chance {
    --node-stroke: #28a745; /* green */
}

#tree-container.tree-colored .node.type-terminal {
    --node-stroke: #1e90ff; /* blue */
}

.node:hover circle, .node:hover rect, .node:hover polygon {
    stroke: #007bff;
}

.node.root-node circle, .node.root-node rect, .node.root-node polygon {
    stroke-width: 3px;
    stroke: #000;
}

.node.root-node:hover circle, .node.root-node:hover rect, .node.root-node:hover polygon {
    stroke: #007bff; /* Hover color */
}

.node.selected circle, .node.selected rect, .node.selected polygon {
    fill: #e6f2ff;
    stroke: #007bff;
    stroke-width: 3px;
}

.node.drag-target circle, .node.drag-target rect, .node.drag-target polygon {
    stroke: #28a745;
    stroke-width: 4px;
    stroke-dasharray: 5, 5;
}

.link {
    fill: none;
    stroke: #999;
    stroke-width: 2px;
}

.link-handle {
    stroke: rgba(120,120,120,0.8);
    stroke-width: 10px; /* big hit area */
    fill: none;
    opacity: 0; /* invisible but clickable */
    cursor: grab;
}

.link-handle:active { cursor: grabbing; }

.editable-text {
    cursor: text;
    fill: #333;
    font-family: inherit;
}

.editable-number {
    cursor: text;
}

.editable-number:hover {
    fill: #007bff;
    text-decoration: underline;
}

.editable-text:hover {
    fill: #007bff;
    text-decoration: underline;
}

.node-value {
    font-weight: bold;
    fill: #000;
    font-size: var(--digits-font-size);
}

.placeholder {
    fill: #777;
    font-weight: 500;
    font-style: italic;
}

.probability-text {
    fill: #444;
    font-size: calc(var(--digits-font-size) - 2px);
}

/* Inline Editor Input */
.inline-editor {
    position: absolute;
    z-index: 100;
    font-family: inherit;
    font-size: 12px;
    padding: 2px 4px;
    border: 1px solid #007bff;
    border-radius: 2px;
    background: white;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
    transform: translate(-50%, -50%); /* Center on click */
    min-width: 60px;
    text-align: center;
}

/* Context Menu */
#context-menu {
    position: absolute;
    background: white;
    border: 1px solid #ccc;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
    padding: 5px 0;
    z-index: 1000;
    display: none;
    border-radius: 4px;
    min-width: 150px;
}

#context-menu .menu-item {
    padding: 8px 15px;
    cursor: pointer;
    font-size: 0.9rem;
    color: #333;
}

#context-menu .menu-item:hover {
    background-color: #f0f0f0;
}

#context-menu .separator {
    height: 1px;
    background-color: #eee;
    margin: 4px 0;
}

/* Tips/Overlay */
#controls-overlay {
    position: absolute;
    bottom: 20px;
    right: 20px;
    background: rgba(255,255,255,0.9);
    padding: 10px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    font-size: 0.85rem;
    color: #555;
    pointer-events: none;
    user-select: none;
}

#controls-overlay { display: none !important; }

#status-bar {
    position: absolute;
    left: 20px;
    bottom: 20px;
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 6px 10px;
    border-radius: 8px;
    font-size: 0.85rem;
    z-index: 900;
    user-select: none;
}

/* Tool button active state */
.toolbar button.tool-active {
    background-color: #1f7a2e;
}

.icon-btn {
    width: 38px;
    height: 34px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon-btn svg {
    width: 18px;
    height: 18px;
}

.toolbar-select {
    height: 34px;
    padding: 0 28px 0 10px;
    border-radius: 6px;
    border: none;
    background-color: #6c757d;
    color: white;
    font-size: 0.9rem;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image:
        linear-gradient(45deg, transparent 50%, white 50%),
        linear-gradient(135deg, white 50%, transparent 50%);
    background-position:
        calc(100% - 14px) 13px,
        calc(100% - 9px) 13px;
    background-size: 6px 6px, 6px 6px;
    background-repeat: no-repeat;
}

.toolbar-select:hover {
    background-color: #5a6268;
}

.color-btn {
    width: 34px;
}

.color-swatch {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #222;
    border: 2px solid rgba(255,255,255,0.65);
    box-shadow: inset 0 0 0 1px rgba(0,0,0,0.35);
}

#notes-layer {
    position: absolute;
    inset: 0;
    pointer-events: none; /* notes will enable pointer events per-note */
}

.sticky-note {
    position: absolute;
    width: 220px;
    min-height: 120px;
    background: #fff7a8;
    border: 1px solid #e2d36f;
    box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    border-radius: 10px;
    padding: 10px;
    pointer-events: auto;
    cursor: grab;
}

.sticky-note.selected {
    outline: 3px solid rgba(0, 123, 255, 0.9);
}

.sticky-note:active { cursor: grabbing; }

.sticky-note textarea {
    width: 100%;
    height: 100%;
    min-height: 100px;
    border: none;
    outline: none;
    resize: none;
    background: transparent;
    font-family: "Times New Roman", Times, serif;
    font-size: 16px;
    line-height: 1.25;
}

.note-resizer {
    position: absolute;
    right: 6px;
    bottom: 6px;
    width: 14px;
    height: 14px;
    border-right: 3px solid rgba(0,0,0,0.35);
    border-bottom: 3px solid rgba(0,0,0,0.35);
    cursor: nwse-resize;
    opacity: 0.7;
}

.annotation-path {
    pointer-events: stroke;
    cursor: pointer;
}

.annotation-path.selected {
    filter: drop-shadow(0 0 2px rgba(0,123,255,0.9));
}
