:root {
    --bg-dark: #0f0f11;
    --bg-gradient-start: #1a1a20;
    --bg-gradient-end: #050506;
    --glass-bg: rgba(30, 30, 35, 0.65);
    --glass-border: rgba(255, 255, 255, 0.08);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
    --accent-color: #3b82f6;
    /* Premium Blue */
    --accent-glow: rgba(59, 130, 246, 0.5);
    --text-primary: #e0e0e0;
    --text-secondary: #9ca3af;
    --tool-hover: rgba(255, 255, 255, 0.1);

    --paper-color: #ffffff;
    --paper-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
    /* Prevent accidental selection while drawing */
}

body {
    background-color: var(--bg-dark);
    color: var(--text-primary);
    font-family: 'Inter', sans-serif;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    position: relative;
    cursor: default;
}

/* Cinematic Background Layers */
#ambient-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at 50% 50%, var(--bg-gradient-start), var(--bg-gradient-end));
    z-index: -2;
    animation: ambient-pulse 10s ease-in-out infinite;
}

#vignette-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: radial-gradient(circle at center, transparent 30%, rgba(0, 0, 0, 0.6) 100%);
    pointer-events: none;
    z-index: 100;
    /* Above everything except tools if needed, but usually overlays for mood */
}

@keyframes ambient-pulse {

    0%,
    100% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.02);
        opacity: 0.95;
    }
}

/* Workspace & Paper */
#workspace {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    cursor: grab;
}

#workspace:active {
    cursor: grabbing;
}

#paper-container {
    width: 794px;
    /* A4 width at 96 DPI approx */
    height: 1123px;
    /* A4 height */
    position: relative;
    background-color: var(--paper-color);
    box-shadow: var(--paper-shadow);
    transform-origin: center;
    transition: transform 0.1s ease-out;
    /* Smooth pan/zoom updates */
}

#drawing-canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
}

#paper-texture {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0IiBoZWlnaHQ9IjQiPgo8cmVjdCB3aWR0aD0iNCIgaGVpZ2h0PSI0IiBmaWxsPSIjZmZmIi8+CjxyZWN0IHdpZHRoPSIxIiBoZWlnaHQ9IjEiIGZpbGw9IiNjY2MiIG9wYWNpdHk9IjAuMSIvPgo8L3N2Zz4=');
    opacity: 0.4;
    pointer-events: none;
    mix-blend-mode: multiply;
}

/* Floating Glass Toolbar */
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    box-shadow: var(--glass-shadow);
    border-radius: 16px;
}

#toolbar {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    width: 64px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 16px 0;
    gap: 12px;
    z-index: 200;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.tool-group,
.action-group,
.system-group,
.color-picker-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
}

.separator {
    width: 32px;
    height: 1px;
    background: rgba(255, 255, 255, 0.1);
    margin: 4px 0;
}

.tool-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

.tool-btn svg {
    width: 24px;
    height: 24px;
    filter: drop-shadow(0 0 0 transparent);
    transition: filter 0.2s ease;
}

.tool-btn:hover {
    background: var(--tool-hover);
    color: var(--text-primary);
    transform: scale(1.05);
}

.tool-btn.active {
    background: var(--tool-hover);
    color: var(--accent-color);
    box-shadow: 0 0 12px var(--accent-glow);
}

.tool-btn.active svg {
    filter: drop-shadow(0 0 5px var(--accent-glow));
}

#color-picker {
    width: 30px;
    height: 30px;
    padding: 0;
    border: none;
    border-radius: 50%;
    overflow: hidden;
    cursor: pointer;
    background: transparent;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
}

#color-picker::-webkit-color-swatch-wrapper {
    padding: 0;
}

#color-picker::-webkit-color-swatch {
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
}

/* Status Bar */
#status-bar {
    position: absolute;
    bottom: 20px;
    right: 20px;
    padding: 8px 16px;
    display: flex;
    gap: 20px;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 500;
    z-index: 200;
}

/* Layer for Physical Tools (Ruler, Compass) */
#tools-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 50;
    overflow: hidden;
}

.physical-tool {
    position: absolute;
    pointer-events: auto;
    cursor: grab;
    filter: drop-shadow(0 10px 20px rgba(0, 0, 0, 0.4));
    transition: transform 0.1s cubic-bezier(0.2, 0.8, 0.2, 1);
    /* Smooth rotation */
}

.physical-tool:active {
    cursor: grabbing;
    transform: scale(1.02);
}

/* Ruler Styling */
#ruler-tool {
    width: 400px;
    height: 70px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 100%);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-bottom: 1px solid rgba(255, 255, 255, 0.3);
    /* Catching light */
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 4px;
    display: flex;
    align-items: flex-end;
    transform-origin: center;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

.ruler-body {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.ruler-markings {
    position: absolute;
    bottom: 0;
    left: 10px;
    right: 10px;
    height: 25px;
    /* Complex CSS gradient to simulate mm/cm lines */
    background:
        repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.9) 0, rgba(255, 255, 255, 0.9) 1px, transparent 1px, transparent 10px),
        /* cm */
        repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.5) 0, rgba(255, 255, 255, 0.5) 1px, transparent 1px, transparent 2px);
    /* mm approx */
    mask-image: linear-gradient(to top, black 60%, transparent 100%);
}

.ruler-info {
    position: absolute;
    top: 8px;
    right: 12px;
    font-size: 11px;
    font-weight: 600;
    font-family: monospace;
    color: rgba(255, 255, 255, 0.9);
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 6px;
    border-radius: 4px;
}

/* Compass Styling */
#compass-tool {
    width: 200px;
    height: 200px;
    /* Pivot center is top center of this box? No, compass structure is complex */
    /* Let's make it a V shape */
    display: flex;
    justify-content: center;
    /* transform-origin should be the needle point */
    transform-origin: 20px 20px;
    /* Assuming pivot is top-leftish */
}

.compass-pivot {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 20px;
    height: 20px;
    background: radial-gradient(circle at 30% 30%, #e0e0e0, #9ca3af);
    border-radius: 50%;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
    z-index: 10;
}

.compass-leg-fixed {
    position: absolute;
    top: 20px;
    left: 18px;
    width: 4px;
    height: 180px;
    background: linear-gradient(90deg, #d1d5db, #9ca3af, #d1d5db);
    transform-origin: top center;
    transform: rotate(-15deg);
    border-radius: 2px;
}

.compass-leg-movable {
    position: absolute;
    top: 20px;
    left: 18px;
    width: 4px;
    height: 180px;
    background: linear-gradient(90deg, #d1d5db, #9ca3af, #d1d5db);
    transform-origin: top center;
    transform: rotate(15deg);
    border-radius: 2px;
    cursor: ew-resize;
    /* Indicate draggable */
}

.compass-pencil-holder {
    position: absolute;
    bottom: 0;
    left: -3px;
    width: 10px;
    height: 30px;
    background: #4b5563;
    border-radius: 2px;
}

/* Protractor Styling */
#protractor-tool {
    width: 300px;
    height: 150px;
    background: linear-gradient(180deg, rgba(200, 220, 255, 0.1) 0%, rgba(200, 220, 255, 0.05) 100%);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-bottom: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 150px 150px 0 0;
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: flex-end;
    transform-origin: bottom center;
}

.protractor-body {
    width: 100%;
    height: 100%;
    position: relative;
    border-radius: 150px 150px 0 0;
    overflow: hidden;
}

.protractor-arc {
    position: absolute;
    bottom: 0;
    left: 10%;
    width: 80%;
    height: 80%;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 50% 50% 0 0;
    border-bottom: none;
}

.protractor-readout {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 16px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

/* Set Square Styling */
#setsquare-tool {
    width: 300px;
    height: 300px;
    /* Triangle shape using clip-path or transparent borders? 
       Real physical look is better with clip-path + background/border simulation 
    */
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 100%);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    clip-path: polygon(0% 100%, 100% 100%, 0% 0%);
    /* Border simulation requires SVG or pseduo elements with clip-path. 
       Simple border doesn't work well with clip-path. 
       We will use a drop-shadow filter on the parent to simulate border depth? No. 
    */
    border-bottom: 2px solid rgba(255, 255, 255, 0.3);
    border-left: 1px solid rgba(255, 255, 255, 0.2);
}

.setsquare-body {
    width: 100%;
    height: 100%;
    position: relative;
    /* Inner hypotenuse line (diagonal) */
}

/* Context Bar */
#context-bar {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 8px 16px;
    display: flex;
    gap: 12px;
    z-index: 200;
}

#text-properties {
    display: flex;
    gap: 8px;
    align-items: center;
}

#font-family {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: 4px;
    outline: none;
    font-family: inherit;
}

#font-size {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: 4px;
    width: 60px;
}

.tool-btn.small {
    width: 30px;
    height: 30px;
    font-size: 14px;
    font-weight: bold;
}

/* Object Layer */
#objects-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* Let clicks pass unless hitting an object */
    z-index: 40;
    /* Below tools, above canvas */
    overflow: hidden;
}

.canvas-object {
    position: absolute;
    pointer-events: auto;
    cursor: grab;
    border: 1px solid transparent;
}

.canvas-object.selected {
    border: 1px solid var(--accent-color);
    box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.3);
}

.resize-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    background: var(--accent-color);
    border-radius: 50%;
    pointer-events: auto;
    display: none;
}

.canvas-object.selected .resize-handle {
    display: block;
}

/* Resize Handle Positions */
.rh-nw {
    top: -5px;
    left: -5px;
    cursor: nw-resize;
}

.rh-ne {
    top: -5px;
    right: -5px;
    cursor: ne-resize;
}

.rh-sw {
    bottom: -5px;
    left: -5px;
    cursor: sw-resize;
}

.rh-se {
    bottom: -5px;
    right: -5px;
    cursor: se-resize;
}

/* Text Object Specifics */
.text-object-content {
    background: transparent;
    border: none;
    outline: none;
    resize: none;
    overflow: hidden;
    color: black;
    /* Set default text color to black for contrast on white paper */
    white-space: pre;
    min-width: 50px;
}