:root {
    --bg-dark: #0f172a;
    --bg-card: rgba(30, 41, 59, 0.7);
    --border-color: rgba(255, 255, 255, 0.1);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;

    --accent: #3b82f6;
    --accent-hover: #60a5fa;

    --true-color: #10b981;
    --false-color: #ef4444;

    --font-family: 'Inter', sans-serif;
}

/* =========================================
   V5 Theme: Detective Red String Evidence
========================================= */
[data-theme="redstring"] {
    --bg-dark: #3e2723;
    /* Dark corkboard brown */
    --bg-card: rgba(255, 235, 153, 0.95);
    /* Sticky note yellow */
    --border-color: rgba(0, 0, 0, 0.3);
    --text-main: #212121;
    /* Dark pen ink */
    --text-muted: #5d4037;

    --accent: #d32f2f;
    /* Red string/pushpin */
    --accent-hover: #f44336;

    --true-color: #388e3c;
    /* Marker green */
    --false-color: #d32f2f;
    /* Marker red */

    --font-family: 'Courier New', monospace;
    /* Typewriter feel */
}

/* Theme D3 Canvas Overrides */
[data-theme="redstring"] .canvas-container {
    background: radial-gradient(circle at center, #5d4037 0%, #3e2723 100%);
}

/* Plain white polaroids for basic nodes */
[data-theme="redstring"] .node circle {
    fill: #f5f5f5 !important;
    stroke: #757575 !important;
    stroke-width: 2px !important;
    filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.5));
}

/* Yellow sticky notes for complex nodes */
[data-theme="redstring"] .node rect {
    fill: #fff59d !important;
    stroke: none !important;
    filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.5));
}

[data-theme="redstring"] .node:hover circle,
[data-theme="redstring"] .node:hover rect {
    stroke: #d32f2f !important;
    stroke-width: 4px !important;
    filter: drop-shadow(0 0 10px rgba(211, 47, 47, 0.8));
}

/* Force all edges to be thick red string */
[data-theme="redstring"] .link {
    stroke: #d32f2f !important;
    stroke-width: 3px !important;
    filter: drop-shadow(1px 2px 2px rgba(0, 0, 0, 0.5));
}

[data-theme="redstring"] .link-marker {
    fill: #d32f2f !important;
}

/* UI Panel Adjustments */
[data-theme="redstring"] .editable-header,
[data-theme="redstring"] .node text {
    font-weight: bold;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-dark);
    color: var(--text-main);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- Typography & Basics --- */
h1 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.5px;
}

h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 1rem;
}

h4 {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.5rem;
}

/* --- Navbar --- */
.navbar {
    height: 60px;
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    z-index: 10;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* --- Buttons --- */
.btn {
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.btn.primary {
    background-color: var(--accent);
    color: white;
}

.btn.primary:hover {
    background-color: var(--accent-hover);
}

.btn.secondary {
    background-color: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.btn.secondary:hover {
    background-color: rgba(255, 255, 255, 0.15);
}

.btn-icon {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
    cursor: pointer;
}

.btn-icon:hover {
    color: var(--text-main);
}

/* --- WASM Status Indicator --- */
.engine-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
    padding-left: 1rem;
    border-left: 1px solid var(--border-color);
}

.indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.indicator.checking {
    background-color: #f59e0b;
    animation: pulse 1.5s infinite;
}

.indicator.ready {
    background-color: var(--true-color);
    box-shadow: 0 0 8px var(--true-color);
}

.indicator.error {
    background-color: var(--false-color);
}

@keyframes pulse {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.4;
    }

    100% {
        opacity: 1;
    }
}

/* --- Workspace & Canvas --- */
.workspace {
    flex: 1;
    position: relative;
    display: flex;
}

.canvas-container {
    flex: 1;
    position: relative;
    background: radial-gradient(circle at center, #1e293b 0%, var(--bg-dark) 100%);
}

#network-canvas {
    width: 100%;
    height: 100%;
}

/* D3.js SVG Styles */
.node circle,
.node rect {
    fill: var(--bg-card);
    stroke: var(--accent);
    stroke-width: 2px;
    transition: all 0.2s;
}

.node rect {
    rx: 8;
    /* Rounded corners */
    ry: 8;
}

.node text {
    fill: var(--text-main);
    font-family: var(--font-family);
    font-size: 14px;
    font-weight: 500;
    pointer-events: none;
}

.node:hover circle,
.node:hover rect {
    stroke: var(--accent-hover);
    stroke-width: 3px;
    filter: drop-shadow(0 0 8px rgba(59, 130, 246, 0.5));
}

.node.selected circle,
.node.selected rect {
    stroke: #fcd34d;
    stroke-width: 3px;
    filter: drop-shadow(0 0 12px rgba(252, 211, 77, 0.4));
}

.node.evidence-set circle,
.node.evidence-set rect {
    fill: rgba(16, 185, 129, 0.2);
    stroke: var(--true-color);
}

.link {
    fill: none;
    stroke: rgba(255, 255, 255, 0.2);
    stroke-width: 2px;
}

.link-marker {
    fill: rgba(255, 255, 255, 0.4);
}

/* --- Floating Properties Panel (Glassmorphism) --- */
.panel {
    position: absolute;
    right: 20px;
    top: 20px;
    width: 320px;
    background: var(--bg-card);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s ease;
}

.panel.hidden {
    transform: translateX(120%);
    opacity: 0;
    pointer-events: none;
}

.panel.left {
    left: 20px;
    right: auto;
}

.panel.left.hidden {
    transform: translateX(-120%);
}

.panel-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.editable-header {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-main);
    font-size: 1rem;
    font-weight: 600;
    font-family: var(--font-family);
    width: 100%;
    margin-right: 10px;
    padding: 4px;
    border-radius: 4px;
    transition: border 0.2s;
}

.editable-header:focus {
    outline: none;
    border: 1px solid var(--accent);
    background: rgba(0, 0, 0, 0.2);
}

.panel-content {
    padding: 1.25rem;
    overflow-y: auto;
    max-height: calc(100vh - 150px);
}

.empty-state {
    color: var(--text-muted);
    font-size: 0.9rem;
    text-align: center;
    padding: 2rem 0;
}

.hidden {
    display: none !important;
}

.section {
    margin-bottom: 1.5rem;
}

/* Toggle Group for Evidence */
.toggle-group {
    display: flex;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    padding: 4px;
    gap: 4px;
}

.toggle-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 6px 0;
    border-radius: 4px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn:hover {
    background: rgba(255, 255, 255, 0.05);
}

.toggle-btn.active[data-state="none"] {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

.toggle-btn.active[data-state="0"] {
    background: var(--true-color);
    color: white;
}

.toggle-btn.active[data-state="1"] {
    background: var(--false-color);
    color: white;
}

/* Progress bars for Probabilities */
.progress-bar-container {
    margin-bottom: 0.75rem;
}

.progress-label {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    margin-bottom: 0.25rem;
}

.progress-track {
    height: 8px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

/* Dynamic color classes for marginal bars */
.color-0 {
    background-color: #10b981;
}

/* Green */
.color-1 {
    background-color: #ef4444;
}

/* Red */
.color-2 {
    background-color: #3b82f6;
}

/* Blue */
.color-3 {
    background-color: #f59e0b;
}

/* Yellow */
.color-4 {
    background-color: #8b5cf6;
}

/* Purple */
.color-5 {
    background-color: #ec4899;
}

/* Pink */
.color-fallback {
    background-color: #94a3b8;
}

/* --- Linking Mode & Edge Types --- */
svg.linking-mode {
    cursor: crosshair;
}

svg.linking-mode .node {
    cursor: copy;
}

.node.linking-source circle {
    stroke: #e879f9;
    /* Pink/Purple for link source */
    stroke-width: 3px;
    stroke-dasharray: 4;
    animation: dash 5s linear infinite;
}

@keyframes dash {
    to {
        stroke-dashoffset: 100;
    }
}

.link.edge-infers {
    stroke: var(--true-color);
}

.link.edge-negates {
    stroke: var(--false-color);
    stroke-dasharray: 5, 5;
}

.link.edge-neutral {
    stroke: rgba(255, 255, 255, 0.2);
}

/* --- CPT Editor Settings --- */
.parent-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    background: rgba(0, 0, 0, 0.2);
    padding: 8px;
    border-radius: 6px;
}

.parent-row select {
    background: var(--bg-dark);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    padding: 4px;
    border-radius: 4px;
    font-size: 0.8rem;
}

.cpt-grid {
    margin-top: 0.5rem;
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 0.5rem;
    font-size: 0.85rem;
    background: rgba(0, 0, 0, 0.2);
    padding: 10px;
    border-radius: 6px;
    max-height: 250px;
    overflow-y: auto;
}

.cpt-row {
    display: contents;
}

.cpt-row>div {
    display: flex;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    padding-bottom: 4px;
}

.cpt-row>input {
    width: 60px;
    background: var(--bg-dark);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 4px;
    text-align: right;
}

.cpt-row>input:focus {
    outline: none;
    border-color: var(--accent);
}

/* --- Tooltips and Modals --- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    border-radius: 12px;
    width: 90%;
    max-width: 600px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}

.modal-header,
.modal-footer {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-footer {
    border-top: 1px solid var(--border-color);
    border-bottom: none;
    justify-content: flex-end;
    gap: 10px;
}

.modal-body {
    padding: 1.25rem;
    overflow-y: auto;
}

.modal-description {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 15px;
}

.error-msg {
    color: var(--danger);
    font-size: 0.85rem;
    margin-top: 10px;
    text-align: center;
}

.row-error {
    border: 1px solid var(--danger) !important;
    background: rgba(239, 68, 68, 0.1) !important;
}

/* --- Robust Windows-Style Sliders --- */
input[type=range] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    background: transparent;
    padding: 10px 0;
    /* Increase hit area above/below the track */
    cursor: pointer;
}

input[type=range]:focus {
    outline: none;
}

/* WebKit (Chrome/Edge/Safari) Track */
input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 8px;
    /* Slightly thicker for better visibility */
    cursor: pointer;
    /* Use CSS variable to dynamically fill track from JS */
    background: linear-gradient(to right, #0078D7 var(--val, 0%), rgba(255, 255, 255, 0.2) var(--val, 0%));
    border-radius: 4px;
    border: none;
}

/* WebKit Thumb */
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    height: 20px;
    width: 20px;
    border-radius: 50%;
    background: #0078D7;
    /* Windows 10/11 Blue Accent */
    cursor: grab;
    margin-top: -7px;
    /* (Track height/2) - (Thumb height/2) */
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.6);
    transition: transform 0.1s ease, background 0.1s ease;
}

input[type=range]::-webkit-slider-thumb:active {
    cursor: grabbing;
    transform: scale(1.15);
    background: #005A9E;
}

/* Firefox Track */
input[type=range]::-moz-range-track {
    width: 100%;
    height: 8px;
    cursor: pointer;
    background: linear-gradient(to right, #0078D7 var(--val, 0%), rgba(255, 255, 255, 0.2) var(--val, 0%));
    border-radius: 4px;
    border: none;
}

/* Firefox Thumb */
input[type=range]::-moz-range-thumb {
    height: 20px;
    width: 20px;
    border-radius: 50%;
    background: #0078D7;
    cursor: grab;
    border: none;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.6);
    transition: transform 0.1s ease, background 0.1s ease;
}

input[type=range]::-moz-range-thumb:active {
    cursor: grabbing;
    transform: scale(1.15);
    background: #005A9E;
}

.slider-container {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 5px;
}

.slider-value {
    font-size: 0.8rem;
    min-width: 40px;
    text-align: right;
}

.slider-label {
    font-size: 0.8rem;
    min-width: 60px;
}